对于POST请求的处理,koa-bodyparser中间件可以把koa2上下文的formData数据解析到ctx.request.body中
npm install --save koa-bodyparser@3
const Koa = require('koa')
const app = new Koa()
const bodyParser = require('koa-bodyparser')
const config = require('../config')
//使用ctx.body解析中间件
app.use(bodyParser())
app.use(async(ctx)=>{
if(ctx.url==='/' && ctx.method==='GET'){
//当GET请求时候返回表单页面
let html = `
<h1>koa2 request post demo</h1>
<form method="POST" action="/">
<p>userName</p>
<input name="userName" /><br/>
<p>nickName</p>
<input name="nickName" /><br/>
<p>email</p>
<input name="email" /><br/>
<button type="submit">submit</button>
</form>
`
ctx.body = html
}else if(ctx.url==='/' && ctx.method=='POST'){
//当post请求的时候,中间件koa-bodyparser解析post表单哩的数据,并显示出来
let postData = ctx.request.body
ctx.body = postData
}else{
//其他请求显示404
ctx.body = '<h1>404.</h1>';
}
})
app.listen( config.port ,()=>{console.log(`端口号为${config.port}的node项目启动成功`);})

koa-post koa-bodyparser