【发布时间】:2017-01-06 08:04:01
【问题描述】:
我想从没有正文解析器中间件的帖子中读取ctx.request.body。
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
app.use(router.routes());
router.post('/publish', async (ctx, next) => {
let msg = JSON.stringify(ctx.request.body);
console.log(msg); //undefined
console.log(ctx.request.body); //undefined
console.log(ctx.req.body); //undefined
});
app.listen(process.env.PORT);
使用 curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' 'http://localhost:8080/publish' 我得到 3 undefined。
我该如何解决这个问题?我知道koa解析不了req.body,但是为什么JSON.stringify(ctx.request.body)不行呢?
【问题讨论】:
-
你为什么不想使用body-parser?
标签: javascript http-post koa2