Express.js req.body undefined

前端 未结 30 2494
半阙折子戏
半阙折子戏 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:24
    // Require body-parser (to receive post data from clients)
    
    var bodyParser = require('body-parser');
    
    app.use(bodyParser.urlencoded({ extended: false }))
    
    // parse application/json
    
    app.use(bodyParser.json())
    
    0 讨论(0)
  • 2020-11-22 12:24

    Wasted a lot of time:

    Depending on Content-Type in your client request
    the server should have different, one of the below app.use():

    app.use(bodyParser.text({ type: 'text/html' }))
    app.use(bodyParser.text({ type: 'text/xml' }))
    app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
    app.use(bodyParser.json({ type: 'application/*+json' }))
    

    Source: https://www.npmjs.com/package/body-parser#bodyparsertextoptions

    Example:

    For me, On Client side, I had below header:

    Content-Type: "text/xml"
    

    So, on the server side, I used:

    app.use(bodyParser.text({type: 'text/xml'}));
    

    Then, req.body worked fine.

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

    In my case, it was because of using body-parser after including the routes.

    The correct code should be

    app.use(bodyParser.urlencoded({extended:true}));
    app.use(methodOverride("_method"));
    app.use(indexRoutes);
    app.use(userRoutes);
    app.use(adminRoutes);
    
    0 讨论(0)
  • 2020-11-22 12:24

    In case if you post SOAP message you need to use raw body parser:

    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    
    app.use(bodyParser.raw({ type: 'text/xml' }));
    
    0 讨论(0)
  • 2020-11-22 12:28
    app.use(express.json());
    

    It will help to solve the issue of req.body undefine

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

    UPDATE July 2020

    express.bodyParser() is no longer bundled as part of express. You need to install it separately before loading:

    npm i body-parser
    
    // then in your app
    var express = require('express')
    var bodyParser = require('body-parser')
     
    var app = express()
     
    // create application/json parser
    var jsonParser = bodyParser.json()
     
    // create application/x-www-form-urlencoded parser
    var urlencodedParser = bodyParser.urlencoded({ extended: false })
     
    // POST /login gets urlencoded bodies
    app.post('/login', urlencodedParser, function (req, res) {
      res.send('welcome, ' + req.body.username)
    })
     
    // POST /api/users gets JSON bodies
    app.post('/api/users', jsonParser, function (req, res) {
      // create user in req.body
    })
    

    See here for further info

    original follows

    You must make sure that you define all configurations BEFORE defining routes. If you do so, you can continue to use express.bodyParser().

    An example is as follows:

    var express = require('express'),
        app     = express(),
        port    = parseInt(process.env.PORT, 10) || 8080;
    
    app.configure(function(){
      app.use(express.bodyParser());
      app.use(app.router);
    });
    
    app.listen(port);
        
    app.post("/someRoute", function(req, res) {
      console.log(req.body);
      res.send({ status: 'SUCCESS' });
    });
    
    0 讨论(0)
提交回复
热议问题