没有req.body发送POST请求

时间:2014-10-07 02:49:13

标签: node.js post express request

在阅读了许多类似的帖子之后,我没有看到我的问题的原因......

我试图学习如何使用node创建RESTful API并表达4,非常基本和简单,但是当我尝试使用POST req保存我的文档时,请求中没有发送req.body,我有一些问题。

让我们看一下代码:

module.exports = (router, Bear) ->
  router.route '/bears'
    .get (req, res) ->
      Bear.find (err, bears) -> if err then err else res.json bears

    .post (req, res) -> 
      newBear =
        _id: req.body.bearId
        nombre: req.body.nombre
      bear = new Bear(newBear)
      bear.save (err) ->
        if err then res.send err 
        # res.json msg: "#{bear.nombre}, creado"
        res.json msg: "created"

到目前为止,这是我的基本路线配置(目前为止)。服务器是:

express = require 'express'
app = express()
morgan = require 'morgan'
bodyParser = require 'body-parser'
mongoose = require 'mongoose'

app.use bodyParser.urlencoded extended: true
app.use bodyParser.json()
app.use morgan 'dev'

require('./app/config/js/database')(mongoose)

router = express.Router()
Bear = require './app/models/js/bear'

router.get '/', (req, res) -> res.json msg: 'It works'

require('./app/routes/js/bears')(router, Bear)

app.use '/api', router

port = 8080

app.listen(port)
console.log("Corriendo en #{port}")

其他代码段在这里(我认为)不相关,因此,当我使用邮递员或CURL发送请求时,我的服务器上始终会收到{}响应和崩溃消息:< / p>

home/nano/Dev/bears/node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.header (/home/nano/Dev/bears/node_modules/express/lib/response.js:662:10)
    at ServerResponse.send (/home/nano/Dev/bears/node_modules/express/lib/response.js:146:12)
    at ServerResponse.json (/home/nano/Dev/bears/node_modules/express/lib/response.js:235:15)
    at Promise.<anonymous> (/home/nano/Dev/bears/app/routes/js/bears.js:22:20)
    at Promise.<anonymous> (/home/nano/Dev/bears/node_modules/mongoose/node_modules/mpromise/lib/promise.js:172:8)
    at Promise.emit (events.js:95:17)
    at Promise.emit (/home/nano/Dev/bears/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.reject (/home/nano/Dev/bears/node_modules/mongoose/node_modules/mpromise/lib/promise.js:111:15)
    at Promise.error (/home/nano/Dev/bears/node_modules/mongoose/lib/promise.js:95:15)

我真的不知道我真的不知道该怎么做,我整个下午都在这里度过。

好吧,我在代码中进行了更改,现在回溯消失了,但请求仍然是空的。

module.exports = (router, Bear) ->
  router.route '/bears'
    .get (req, res) ->
      Bear.find (err, bears) -> 
        if err then return res.send err 
        res.json bears

    .post (req, res) -> 
      newBear =
        _id: req.body.bearId
        nombre: req.body.nombre
      bear = new Bear(newBear)
      bear.save (err) ->
        if err then return res.send err
        res.json msg: "created"

JS代码:

(function() {
  module.exports = function(router, Bear) {
    return router.route('/bears').get(function(req, res) {
      return Bear.find(function(err, bears) {
        if (err) {
          return res.send(err);
        }
        return res.json(bears);
      });
    }).post(function(req, res) {
      var bear, newBear;
      newBear = {
        _id: req.body.bearId,
        nombre: req.body.nombre
      };
      bear = new Bear(newBear);
      return bear.save(function(err) {
        if (err) {
          return res.send(err);
        }
        return res.json({
          msg: "created"
        });
      });
    });
  };

}).call(this);

我通过RESTClient for Firefox,Curl,Postman发送请求......内容类型标题为Content-Type: application/json; charset=utf-8

1 个答案:

答案 0 :(得分:0)

如果你发表评论:

Bear = require './app/models/js/bear'

并替换:

require('./app/routes/js/bears')(router, Bear)

使用:

router.post "/bears", (req, res, next) ->
  console.log req.body
  res.json req.body
  return

你会看到req.body正在通过。

正如评论中所述,您的 bears.coffee 路由文件中缺少返回语句。您需要更改第12行,使其显示为:

if err then return res.send err

确保您在请求正文中发送参数。对于卷曲,你应该使用:

curl -X POST -d "bearId=1221&nombre=papa" http://localhost:8080/api/bears

对于Postman,请确保从下拉列表中选择POST,然后单击x-www-form-urlencoded并将参数放入&#34; Key&#34;和&#34;价值&#34;分别是。