npm install -g babel-cli
npm install koa@v2.0.0-alpha.3
(更新本文时的最新版本为2.0 alpha)
最新版本: https://github.com/koajs/koa
app.js:
const Koa = require('koa');
const app = new Koa();
// logger
app.use(async (ctx, next) => {
const start = new Date;
await next();
const ms = new Date - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
// response
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
注: 需要Stage-3支持
npm install babel-preset-stage-3
执行:
babel-node --presets stage-3 app.js