简体   繁体   中英

Koa2 request.body is empty

I am working on a web service with koa2 and node6. My koa dependencies as follows;

"koa": "^2.0.0-alpha.4",
"koa-async-body": "^1.0.4",
"koa-bodyparser": "^3.2.0",
"koa-logger": "^1.3.0",
"koa-router": "^7.0.1",

My implementation is like this;

 const apiPrefix = 'api', apiParent = 'auth', api = 'register', router = new Router(); router.prefix(`/${apiPrefix}/${apiParent}/${api}`); router.post('/', async(context, next) => { try { console.log(context.request.body); context.body = await post(context.request.body); await next(); } catch (err) { context.throw(500); } }); 

In another class, I bind this route to app. Also I have added bodyParser to the Koa as follows;

const app = new Koa();

app.use(bodyParser());

When I try to log the request body, it's an empty object. On the other hand, this setup works fine with other people in this project.

What am I doing wrong? Am I using an outdated dependency?

Content-type: application/json标头添加到我的Postman请求中,解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM