koa 安装使用

安装:

npm install --save  koa

const Koa = require('koa'); //引入koa模块
const app = new Koa(); //实例化

// 中间件
app.use(ctx => { //ctx,包含req和res等信息,是一个context上下文
    ctx.body = 'Hello Koa'; //类似于express中的res.send,原生NodeJS中的 res.writeHead()和res.end()
});

app.listen(3000);

浏览器打开3000端口,显示“ Hello Koa ”

 

posted @ 2021-02-01 23:07  shanlu  阅读(106)  评论(0编辑  收藏  举报