koa2怎么获取post请求参数? 补充:我是用fetch发起post请求的,试了加入koa-body和koa-body-parser中间件都不行
使用postman或curl测试
@i5ting 是koa2不支持这些中间件了吗
你可不用中间件 let postdata = “”; ctx.req.addListener(‘data’, (data) => { postdata += data }) ctx.req.addListener(“end”, function() { console.log(postdata); }) ```
https://www.npmjs.com/package/koa-bodyparser
var Koa = require(‘koa’); var bodyParser = require(‘koa-bodyparser’);
var app = new Koa(); app.use(bodyParser());
app.use(async ctx => { // the parsed body will store in ctx.request.body // if nothing was parsed, body will be an empty object {} ctx.body = ctx.request.body; });