Express.js req.body undefined

前端 未结 30 2493
半阙折子戏
半阙折子戏 2020-11-22 12:02

I have this as configuration of my Express server

app.use(app.router); 
app.use(express.cookieParser());
app.use(express.session({ secret: \"keyboard cat\" }         


        
相关标签:
30条回答
  • 2020-11-22 12:19

    In case anyone runs into the same issue I was having; I am using a url prefix like

    http://example.com/api/
    

    which was setup with router

    app.use('/api', router); 
    

    and then I had the following

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    

    What fixed my issue was placing the bodyparser configuration above app.use('/api', router);

    Final

    // setup bodyparser
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: true }));
    
    //this is a fix for the prefix of example.com/api/ so we dont need to code the prefix in every route
        app.use('/api', router); 
    
    0 讨论(0)
  • 2020-11-22 12:19

    express.bodyParser() needs to be told what type of content it is that it's parsing. Therefore, you need to make sure that when you're executing a POST request, that you're including the "Content-Type" header. Otherwise, bodyParser may not know what to do with the body of your POST request.

    If you're using curl to execute a POST request containing some JSON object in the body, it would look something like this:

    curl -X POST -H "Content-Type: application/json" -d @your_json_file http://localhost:xxxx/someRoute
    

    If using another method, just be sure to set that header field using whatever convention is appropriate.

    0 讨论(0)
  • 2020-11-22 12:20

    Building on @kevin-xue said, the content type needs to be declared. In my instance, this was only occurring with IE9 because the XDomainRequest doesn't set a content-type, so bodyparser and expressjs were ignoring the body of the request.

    I got around this by setting the content-type explicitly before passing the request through to body parser, like so:

    app.use(function(req, res, next) {
        // IE9 doesn't set headers for cross-domain ajax requests
        if(typeof(req.headers['content-type']) === 'undefined'){
            req.headers['content-type'] = "application/json; charset=UTF-8";
        }
        next();
    })
    .use(bodyParser.json());
    
    0 讨论(0)
  • 2020-11-22 12:21

    Add in your app.js

    before the call of the Router

    const app = express();
    app.use(express.json());
    
    0 讨论(0)
  • 2020-11-22 12:21

    To work, you need to app.use(app.router) after app.use(express.bodyParser()), like that:

    app.use(express.bodyParser())
       .use(express.methodOverride())
       .use(app.router);
    
    0 讨论(0)
  • 2020-11-22 12:22

    This issue may be because you have not use body-parser (link)

    var express = require('express');
    var bodyParser  = require('body-parser');
    
    var app = express();
    app.use(bodyParser.json());
    
    0 讨论(0)
提交回复
热议问题