我已经看过几篇与此相关的文章,但没有一篇似乎是这里的问题。expressjs req.body.username is undefined
这是一个非常简单的初学者用的NodeJS应用expressjs Windows7上
/**
* Module dependencies.
*/
var express = require('express')
, connect = require('connect')
, http = require('http')
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
// app.use(express.bodyParser());
app.use(connect.bodyParser());
});
/**
* Routes
*/
//get requests
// app.get("/", function(req, res){
// res.send("Hello, Express!");
// });
// app.get("/hi", function(req, res){
// var message = [
// "<h1>Hello, Express!</h1>",
// "<p>Welcome to 'Building Web Apps in Node.js with Express.'</p>",
// "<p>You'll love Express because it's</p>",
// "<ul><li>Fast</li>",
// "<li>Fun</li>",
// "<li>Flexible</li>"
// ].join("\n");
// res.send(message);
// });
// app.get("https://stackoverflow.com/users/:userID", function(req, res){
// res.send("<h1>Hello, User #" + req.params.userID + "!");
// });
//post requests
app.post("/users", function(req, res){
// res.send(req.body);
res.send(req.body.username);
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
正如你可以看到我已经注释掉其他路线只是,以确保它们不会导致问题,我也尝试安装并使用'连接',但express.bodyParser()和connect.bodyParser()给我相同的结果。
我已经使用chrome adv rest客户端扩展,并且还尝试了以下简单的php表单。
<form action="http://localhost:3000/users" method="POST" enctype="application/x-www-form-urlencoded">
<li>
<label for="username">Username</label>
<input type="text" name="username" id="username">
</li>
<li>
<input type="submit" name="submit" id="submit">
</li>
</form>
在任何情况下,我得到一个空的或不确定的req.body.username,当我尝试只是req.body在响应我得到一个空对象{}
UPDATE: 这里是从铬响应/请求头信息:
Request URL:http://localhost:3000/users
Request Method:POST
Status Code:200 OK
Request Headersview source
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:31
Host:localhost:3000
Request Payload
username=isimmons&submit=Submit
Response Headersview source
Connection:keep-alive
Date:Sat, 27 Oct 2012 23:01:45 GMT
Transfer-Encoding:chunked
X-Powered-By:Express
,这里是我所得到的,当我送req.route
//result of console.log(req.route);
{ path: '/users',
method: 'post',
callbacks: [ [Function] ],
keys: [],
regexp: /^\/users\/?$/i,
params: [] }
节点和快速的版本: 表达3.0 节点0.8.9
UPDATE2问题SEMI解决 见下面的评论。我应该在这里说过。无论如何,这是当它正常工作时,来自chrome的请求标题信息。
Request URL:http://localhost:3000/users
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:26
Content-Type:application/x-www-form-urlencoded
Host:localhost:3000
Origin:http://localhost
Referer:http://localhost/nodeform.php
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.16 (KHTML, like Gecko) Chrome/24.0.1305.3 Safari/537.16
Form Dataview sourceview URL encoded
username:dfd
submit:Submit
Response Headersview source
Connection:keep-alive
Content-Length:3
Content-Type:text/html; charset=utf-8
Date:Sun, 28 Oct 2012 01:12:23 GMT
X-Powered-By:Express
感谢
您可以删除连接东西,使用express.bodyParser()。 Express是连接之上的一层,因此您不需要直接使用它。我的代码中没有看到任何奇怪的东西。我唯一能想到的是,因为app.configure在windows上导致问题。你可以删除app.configure。这是糖检查NODE_ENV并根据该值执行该功能。 – Pickels
您的代码似乎没有任何明显的问题。你安装了什么版本的Express?而且,自从您提到Chrome以来,您是否[检查了](https://developers.google.com/chrome-developer-tools/docs/network)“
@Pickels是的我看到周围后,连接已经是明确的node_module,所以我删除了所有这一切。但是,当我删除该端口的配置行时,我找不到页面,因为它似乎没有对端口3000进行默认设置。 – isimmons