简体   繁体   English

无法使用Node.js和Express框架读取post参数

[英]Cannot read post parameters with Node.js and Express framework

I have a node.js server with express Framework. 我有一个带有express Framework的node.js服务器。

var express = require('express');
var http = require('http');
var api_helper = require('./helpers/api_helper');
var app = express();

app.use(app.router);
app.use(express.bodyParser());
app.set('port', process.env.PORT || 8081);

app.post('/api/nodev1/users/login', function(req, res){
  var email = req.param('email', null);
  var password = req.param('password', null);
  console.log(email);console.log(password);
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

When I try to post a request to /api/nodev1/users/login I cannot read the parameters. 当我尝试将请求发布到/ api / nodev1 / users / login时,无法读取参数。 I am trying with curl as follows: 我正在尝试卷曲如下:

curl -d "email=user@example2.com" -d "password=mypassword" http://localhost:8081/api/nodev1/users/login

The email and password are undefined. 电子邮件和密码未定义。

You have to move app.use(app.router) below app.use(express.bodyParser()) . 你必须将app.use(app.router) app.use(express.bodyParser()) app.router is just a hook in which stage to handle your routes. app.router只是在哪个阶段处理路由的一个钩子。 And if it comes before bodyParser the body is not parsed. 如果它出现在bodyParser之前, bodyParser身体就不会被解析了。

Your code could look like this (in case I didn't manage to explain understandable): 您的代码可能如下所示(以防万一我无法解释清楚):

var app = express();

app.use(express.bodyParser());
app.set('port', process.env.PORT || 8081);

app.use(app.router);

// I added following line so you can better see what happens
app.use(express.logger('dev'));

app.post('/api/nodev1/users/login', function(req, res){ ... }

Offtopic remark : express.bodyParser() should only be used when you have file-uploads. 离题备注express.bodyParser()仅在有文件上传时使用。 And then you have to take care of deleting temp-files. 然后你必须要注意删除临时文件。 If you don't have file-uploads, you are better off with only 如果没有文件上传,最好只使用

app.use(express.json());
app.use(express.urlencoded());

I just wanted to add this in case you didn't know. 我只是想在你不知道的情况下添加它。 I ran in problems because I didn't know... 我遇到了问题,因为我不知道...

Edit for Express 4 编辑Express 4

Thanks to @jonathan-ong there is no app.use(app.router) since Express 4: 由于@ jonathan-ong,自Express 4起就没有app.use(app.router)

All routing methods will be added in the order in which they appear. 所有路由方法都将按其出现的顺序添加。 You should not do app.use(app.router) . 应该做app.use(app.router) This eliminates the most common issue with Express. 这样可以消除Express中最常见的问题。

In other words, mixing app.use() and app[VERB]() will work exactly in the order in which they are called. 换句话说,将app.use()app[VERB]()完全按照它们被调用的顺序进行。

Read more: New features in 4.x . 阅读更多:4.x中的新功能

edit - nope, see other answer about middleware order! 编辑 -不,请参阅有关中间件顺序的其他答案!


change req.param to req.body.x : req.param更改为req.body.x

app.post('/api/nodev1/users/login', function(req, res){
  var email = req.param('email', null);
  var password = req.param('password', null);
  console.log(email);console.log(password);
});

to

app.post('/api/nodev1/users/login', function(req, res){
  var email = req.body.email);
  var password = req.body.password);
  console.log(email); console.log(password);
});

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

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