node.js Express.js req.body 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9177049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:08:29  来源:igfitidea点击:

Express.js req.body undefined

node.jsexpress

提问by Masiar

I have this as configuration of my Express server

我有这个作为我的 Express 服务器的配置

app.use(app.router); 
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("view options", { layout: true });
//Handles post requests
app.use(express.bodyParser());
//Handles put requests
app.use(express.methodOverride());

But still when I ask for req.body.somethingin my routes I get some error pointing out that body is undefined. Here is an example of a route that uses req.body:

但是当我req.body.something在我的路线中要求时,我仍然得到一些错误指出body is undefined。以下是使用 的路由示例req.body

app.post('/admin', function(req, res){
    console.log(req.body.name);
});

I read that this problem is caused by the lack of app.use(express.bodyParser());but as you can see I call it before the routes.

我读到这个问题是由缺乏引起的,app.use(express.bodyParser());但是正如你所看到的,我在路由之前调用它。

Any clue?

有什么线索吗?

采纳答案by Masiar

As already posted under one comment, I solved it using

正如已经在一条评论下发布的那样,我使用

app.use(require('connect').bodyParser());

instead of

代替

app.use(express.bodyParser());

I still don't know why the simple express.bodyParser()is not working...

我仍然不知道为什么简单express.bodyParser()的不起作用......

回答by Mark Bonano

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

您必须确保在定义路由之前定义所有配置。如果这样做,您可以继续使用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' });
});

回答by Jay

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

最新版本的 Express (4.x) 已将中间件从核心框架中分离出来。如果需要body parser,需要单独安装

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())

回答by danmactough

No. You need to use app.use(express.bodyParser())before app.use(app.router). In fact, app.use(app.router)should be the last thing you call.

不,您需要app.use(express.bodyParser())app.use(app.router). 事实上,app.use(app.router)应该是你打电话的最后一件事。

回答by Ankit kaushik

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

首先确保您已经通过调用安装了名为“body-parser”的 npm 模块:

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());

回答by Kevin Xue

The Content-Type in request header is really important, especially when you post the data from curl or any other tools.

请求头中的 Content-Type 非常重要,尤其是当您从 curl 或任何其他工具发布数据时。

Make sure you're using some thing like application/x-www-form-urlencoded, application/json or others, it depends on your post data. Leave this field empty will confuse Express.

确保您使用的是 application/x-www-form-urlencoded、application/json 或其他内容,这取决于您的帖子数据。将此字段留空会混淆 Express。

回答by TechTurtle

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

Express 4,具有内置的正文解析器。无需安装单独的 body-parser。所以下面将起作用:

export const app = express();
app.use(express.json());

回答by ASHISH RANJAN

// 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())

回答by abdesselam

Add in your app.js

加入你的 app.js

before the call of the Router

在路由器调用之前

const app = express();
app.use(express.json());

回答by Praneesh

Looks like the body-parser is no longer shipped with express. We may have to install it separately.

看起来身体解析器不再随快递一起提供。我们可能需要单独安装它。

var express    = require('express')
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())

// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
app.use(function (req, res, next) {
console.log(req.body) // populated!

Refer to the git page https://github.com/expressjs/body-parserfor more info and examples.

有关更多信息和示例,请参阅 git 页面https://github.com/expressjs/body-parser