【问题标题】:Nodejs - Req.body undefined in post with express 4.9.0Nodejs - 在 Express 4.9.0 的帖子中未定义 Req.body
【发布时间】:2015-02-13 15:34:16
【问题描述】:

我是 nodejs 的初学者,我尝试使用中间件 body-parse 或什么都不使用来了解 req.body,但都发现 req.body 是未定义的。这是我的代码

var app = require('express')();         
var bodyParser = require('body-parser');
var multer = require('multer');         

app.get('/', function(req, res) {       
    res.send("Hello world!\n");         
});                                     

app.post('/module', function(req, res) {
    console.log(req);                   
    app.use(bodyParser.json());         
    app.use(bodyParser.urlencoded({     
        extended: true                  
    }));                                
    app.use(multer);                    
    console.log(req.body);              
});                                     

app.listen(3000);                       

module.exports = app;  

我使用命令curl -X POST -d 'test case' http://127.0.0.1:3000/module 来测试它。

express的版本:4.9.0
节点版本:v0.10.33

请帮忙,谢谢。

【问题讨论】:

  • 我认为 bodyParser 提取了一个 json 而 test case 不是:/
  • 移动 app.use(bodyParser.json()); (以及其余的中间件)来自 /post 处理程序
  • 我试过 curl -X POST -d '{"name":"tyrion"}' 127.0.0.1:3000/module,但仍然未定义。

标签: javascript node.js curl express


【解决方案1】:

默认情况下,cURL 使用Content-Type: application/x-www-form-urlencoded 提交不包含文件的表单。

对于 urlencoded 表单,您的数据需要采用正确的格式:curl -X POST -d 'foo=bar&baz=bla' http://127.0.0.1:3000/module 或 curl -X POST -d 'foo=bar' -d 'baz=bla' http://127.0.0.1:3000/module。

对于 JSON,您必须明确设置正确的 Content-Type: curl -H "Content-Type: application/json" -d '{"foo":"bar","baz":"bla"}' http://127.0.0.1:3000/module。

正如@Brett 所指出的,您需要app.use() 您的中间件在 POST 路由某处(路由处理程序之外)。

【讨论】:

    【解决方案2】:

    您将 body-parser 的快速配置放置在错误的位置。

    var app = require('express')();         
    var bodyParser = require('body-parser');
    var multer = require('multer');         
    
    // these statements config express to use these modules, and only need to be run once
    app.use(bodyParser.json());         
    app.use(bodyParser.urlencoded({ extended: true }));                                
    app.use(multer);
    
    // set up your routes
    app.get('/', function(req, res) {       
        res.send("Hello world!\n");         
    });                                     
    
    app.post('/module', function(req, res) {
        console.log(req);                                       
        console.log(req.body);              
    });                                     
    
    app.listen(3000);                       
    
    module.exports = app;  
    

    【讨论】:

    • 谢谢你的帮助,我按照你说的做了,但是注意到打印到终结符,我不知道为什么。
    • 感谢您的帮助,我发现代码错误,app.use(multer())。
    【解决方案3】:

    您必须确保在定义路由之前定义所有快速配置。因为body-parser负责解析requst的body。

    var express = require('express'),
        app     = express(),
        port    = parseInt(process.env.PORT, 10) || 8080;
    
    //you can remove the app.configure at all in case of it is not supported
    //directly call the inner code     
    app.configure(function(){
      app.use(bodyParser.urlencoded());
      //in case you are sending json objects 
      app.use(bodyParser.json());
      app.use(app.router);
    });
    
    app.listen(port);
    
    app.post("/module", function(req, res) {
      res.send(req.body);
    });
    

    【讨论】:

    • express 4.9中没有app.configure
    猜你喜欢
    • 2014-09-05
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多