写一个简单的 Koa
我们先基于 node 最基本的 http API 来启动一个 http 服务,并通过它来实现最简版的 koa:
const http = require('http')
const server = http.createServer((req,res)=>{
res.end('hello world')
})
server.listen(3000)
而要实现最简版的 koa 示例如下,我把最简版的这个 koa 命名为 koa-mini
const Koa = require('koa')
const app = new Koa()
app.use(async (ctx, next) => {
console.log("Middleware 1 Start");
await next();
console.log("Middleware 1 End");
ctx.body = "hello, world";
});
app.listen(3000)