Express.js req.body undefined

前端 未结 30 2495
半阙折子戏
半阙折子戏 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:29

    Latest versions of Express (4.x) has unbundled the middleware from the core framework. If you need body parser, you need to install it separately

    npm install body-parser --save
    

    and then do this in your code

    var bodyParser = require('body-parser')
    var app = express()
    
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))
    
    // parse application/json
    app.use(bodyParser.json())
    
    0 讨论(0)
  • 2020-11-22 12:30

    You can use express body parser.

    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({ extended: true }));
    
    0 讨论(0)
  • 2020-11-22 12:32

    First make sure , you have installed npm module named 'body-parser' by calling :

    npm install body-parser --save
    

    Then make sure you have included following lines before calling routes

    var express = require('express');
    var bodyParser = require('body-parser');
    var app = express();
    
    app.use(bodyParser.json());
    
    0 讨论(0)
  • 2020-11-22 12:32

    This occured to me today. None of above solutions work for me. But a little googling helped me to solve this issue. I'm coding for wechat 3rd party server.

    Things get slightly more complicated when your node.js application requires reading streaming POST data, such as a request from a REST client. In this case, the request's property "readable" will be set to true and the POST data must be read in chunks in order to collect all content.

    http://www.primaryobjects.com/CMS/Article144

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

    This is also one possibility: Make Sure that you should write this code before the route in your app.js(or index.js) file.

    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    
    0 讨论(0)
  • 2020-11-22 12:36

    Express 4, has build-in body parser. No need to install separate body-parser. So below will work:

    export const app = express();
    app.use(express.json());
    
    0 讨论(0)
提交回复
热议问题