5219090
koa2怎么获取post请求参数? 补充:我是用fetch发起post请求的,试了加入koa-body和koa-body-parser中间件都不行
koa2怎么获取post请求参数? 补充:我是用fetch发起post请求的,试了加入koa-body和koa-body-parser中间件都不行
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; });
你可不用中间件 let postdata = ""; ctx.req.addListener('data', (data) => { postdata += data }) ctx.req.addListener("end", function() { console.log(postdata); }) ```
使用postman或curl测试