简体   繁体   English

发布请求问题

[英]making post request issue

I'm trying to print the data received from the body yet not working.我正在尝试打印从身体接收到的数据但无法正常工作。 Here is my attempt ...这是我的尝试...

router.post('/api/main',(req,res) => {

  if(req.error){
    res.status(400).send(error.details[0].message);
    return;
  }
   res.send(req.body.message)
})

on postmen Im making a post request like these: localhost:5000/api/main/ the body looks like these: JSON在邮递员上,我发出这样的帖子请求: localhost:5000/api/main/正文如下所示: JSON

{
  "message": "hello"
}

However, im getting this response然而,我得到了这个回应

{
    "success": false,
    "msg": {}
}

What am I missing我错过了什么

Add body parser as middleware on your post router.正文解析器添加为 post 路由器上的中间件。

Here's the proper way to set your body parser middleware.这是设置正文解析器中间件的正确方法。

router.use(bodyParser.urlencoded({extended: true});
router.post('/api/main', (req, res) ...)

You may also want to use bodyParser.json()您可能还想使用bodyParser.json()

在您的索引文件中,在使用任何路由器之前,添加 app.use(bodyParser.urlEncoded({extended: true}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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