首先在项目中安装koa
npm install koa --save
新建一个Js文件
const koa = require('koa')
const app = new koa()
// 使用
// app.use(async (ctx) => {
// ctx.body = 'hello koa'
// })
// 中间件机制
app.use(async (ctx, next) => {
ctx.body = '1'
next()
ctx.body += '2'
})
app.use(async (ctx, next) => {
ctx.body += '3'
next()
ctx.body += '4'
})
app.use(async (ctx, next) => {
ctx.body += '5'
next()
ctx.body += '6'
})
// 监听端口 3000 可以自定义
app.listen(3000)
ctx是上下文,next是执行下一个中间件
node运行js文件
浏览器访问 http://