Koa2 request.body是空的 [英] Koa2 request.body is empty

查看:1769
本文介绍了Koa2 request.body是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用koa2和node6进行Web服务。我的koa依赖如下:

 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,

我的实现是这样的;



  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);}}) ;  



在另一个类中,我将此路由绑定到应用程序。另外我已经将bodyParser添加到Koa,如下所示:

  const app = new Koa(); 

app.use(bodyParser());

当我尝试记录请求正文时,它是一个空对象。另一方面,这个设置在这个项目中与其他人一样正常。



我做错了什么?我使用过时的依赖关系?

解决方案

添加内容类型:application / json header到我的邮递员请求,解决了这个问题。


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?

解决方案

Adding Content-type: application/json header to my Postman request, resolved the issue.

这篇关于Koa2 request.body是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆