执行koa用到async的时候需要babel环境,这里稍微简单的封装了一下,以便于更简单的使用koa的时候,不用在意babel的细节
它是koa-generator的核心组件
node --harmony-async-await
npm i -g runkoa
runkoa app.js
require('runkoa')(__dirname + '/app.js' )
koa2支持3种写法,分别测试如下
Koa is an middleware framework, it can take 3 different kind function as middleware:
Here we write an logger middleware with different function.
runkoa app.js
runkoa app-async.js
runkoa app-generator.js
代码变动,使用supervisor或nodemon自动重启服务是开发里比较好的实践,runkoa可以非常好的和它们进行集成,让开发更简单
首先通过runkoa来加载bin/www
里,代码如下
require('runkoa')(__dirname + '/app.js')
然后执行
nodemon bin/www
此时,你可以放心的去修改你的代码了
touch bin/run
var current_path = process.cwd();
require('runkoa')(current_path + '/bin/www' )
然后执行pm2即可
#npm run pm2
pm2 start bin/run
➜ koa-demo runkoa index.js
index.js
/Users/sang/workspace/17koa/koa-demo/node_modules/babel-polyfill/lib/index.js:14
throw new Error("only one instance of babel-polyfill is allowed");
^
Error: only one instance of babel-polyfill is allowed
at Object.<anonymous> (/Users/sang/workspace/17koa/koa-demo/node_modules/babel-polyfill/lib/index.js:14:9)
at Module._compile (module.js:434:26)
at Module._extensions..js (module.js:452:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/sang/workspace/github/runkoa/node_modules/babel-register/lib/node.js:134:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (index.js:3:1)
at Module._compile (module.js:434:26)
➜ koa-demo cat index.js
'use strict';
require("babel-polyfill");
// set babel in entry file
require('babel-core/register')({
presets: ['es2015-node5', 'stage-3']
});
require('./app'); // this is es7 - gets transpiled
这时正确的操作是
runkoa app.js
依赖
请保持你的package.json或node_modules目录下无这些依赖即可
这是因为runkoa需要指定babel需要的preset地址
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)i5ting
i5ting@126.com如有建议或意见,请在issue提问或邮件
this repo is released under the MIT License.