简体   繁体   English

快递请求正文未定义

[英]Express request body for post is undefined

I have the following setup in my app.js :我的app.js中有以下设置:

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var router = express.Router();
require('./routes/index')(router);
app.use(router);

In my routes/index.js I have all the routes defined:在我的routes/index.js中,我定义了所有路线:

module.exports = function (router) {
    /* GET home page. */
    router.get('/', function(req, res, next) {
        res.render('index', { title: 'Home' });
    });
    router.post('/', function(req, res, next) {
        console.log(req.body);
    });
}

Then in my app entry point bin/server.js :然后在我的应用程序入口点bin/server.js中:

var app = require('../app');
var debug = require('debug')('NodeJSDemo:server');
var http = require('http');
var port = 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);

When I make a POST call on http://localhost:3000/ with a request body, in the console log console log request body is undefined.当我使用请求正文在http://localhost:3000/上进行 POST 调用时,在控制台日志中未定义控制台日志请求正文。

Is there anything wrong with my setup?我的设置有什么问题吗? From this post Express.js req.body undefined it seems as long as I call从这篇文章Express.js req.body undefined看来只要我打电话

app.use(bodyParser.json()) app.use(bodyParser.json())

before loading routes, it should be fine but seems like it does not.在加载路线之前,它应该没问题,但似乎没有。

The problem arises from what type of resource you are sending on your POST request.问题出在您在POST请求中发送的资源类型。

Your bodyParser.json() is ONLY parsing json format.您的bodyParser.json()仅解析json格式。 Or in other words if you simply POST a simple form that in most cases defaults to application/x-www-form-urlencoded you will not get the body object.或者换句话说,如果您只是简单地发布一个在大多数情况下默认为application/x-www-form-urlencoded的简单表单,您将不会获得 body 对象。

As it is stated in the documentation:正如文档中所述:

Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option.返回仅解析 json 且仅查看 Content-Type 标头与类型选项匹配的请求的中间件。

and

type - The type option is used to determine what media type the middleware will parse. type - type 选项用于确定中间件将解析的媒体类型。 Defaults to application/json.默认为应用程序/json。

The solution would be to implement and cather from other scenarios so:解决方案是实施和收集其他场景,因此:

application/x-www-form-urlencoded应用程序/x-www-form-urlencoded

app.use(bodyParser.urlencoded())

multipart/form-data多部分/表单数据

Express does not parse multipart bodies as stated in the documentation:如文档中所述,Express 不会解析多部分正文:

This does not handle multipart bodies, due to their complex and typically large nature.这不处理多部分主体,因为它们复杂且通常很大。 For multipart bodies, you may be interested in the following modules:对于多部分主体,您可能对以下模块感兴趣:

  1. busboy and connect-busboy busboyconnect-busboy
  2. multiparty and connect-multiparty多方连接多方
  3. formidable强大
  4. multer多个

For Express versions: +4.17.0对于 Express 版本:+4.17.0

You could not include the bodyParser dependency.您不能包含 bodyParser 依赖项。 And use the express built-in methods as:并使用 express 内置方法:

express.json()   
express.text() 
express.urlencoded()

They are built based on the bodyParser module so instead calling bodyParser.json() .它们是基于 bodyParser 模块构建的,因此调用bodyParser.json() You would do express.json() and achieve the same results.你会做express.json()并获得相同的结果。

Source: https://expressjs.com/en/resources/middleware/body-parser.html#bodyparserurlencodedoptions资料来源: https ://expressjs.com/en/resources/middleware/body-parser.html#bodyparserurlencodedoptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM