koa
https://raw.githubusercontent.com/koajs/koa/master/docs/logo.png https://koajs.com
HandlerをMiddlewareの集合として定義する仕組みを持っており大変シンプル
こんな雰囲気(公式サイトより)
code:app.js
const Koa = require('koa');
const app = new Koa();
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(${ctx.method} ${ctx.url} - ${rt});
});
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', ${ms}ms);
});
// response
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
各Middleware内でnextを呼ぶことで次のMiddlewareに処理を移譲しつつ、協調してHandlingできるのが特徴