Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js expressjs req.body.username未定义_Node.js_Express - Fatal编程技术网

Node.js expressjs req.body.username未定义

Node.js expressjs req.body.username未定义,node.js,express,Node.js,Express,我看过好几篇与此相关的文章,但这里似乎没有一篇是问题所在 这是一款在windows7上使用expressjs的非常简单的初学者nodejs应用程序 /** * Module dependencies. */ var express = require('express') , connect = require('connect') , http = require('http') var app = express(); app.configure(functio

我看过好几篇与此相关的文章,但这里似乎没有一篇是问题所在

这是一款在windows7上使用expressjs的非常简单的初学者nodejs应用程序

/**
 * 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("/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'));
});
这是我发送req.route时得到的结果

//result of console.log(req.route);
{ path: '/users',
  method: 'post',
  callbacks: [ [Function] ],
  keys: [],
  regexp: /^\/users\/?$/i,
  params: [] }
节点和Express版本: express 3.0 节点0.8.9

更新2个半解决的问题 见下面的评论。我应该在这里说的。无论如何,这是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

谢谢

Ok,终于找到了RESTAPI的错误报告,只需添加内容类型就解决了问题

错误报告有一个回复,可以在发送之前手动添加“Content type”标题。这就是php表单工作的原因。它设置了enctype,所以我得到了完整的请求头,如上图所示

要手动添加的正确内容类型标题为

Content-Type: application/x-www-form-urlencoded

您可以删除connect内容并使用express.bodyParser()。Express是位于connect之上的一层,因此您不需要直接使用它。我看不出你剩下的代码有什么奇怪的地方。我能想到的唯一一件事是,因为app.configure在windows上造成了问题。您可以删除app.configure。是sugar检查节点_ENV并根据值执行函数。代码似乎没有任何明显的问题。您安装了什么版本的Express?而且,既然你提到了Chrome,你有没有关于
的POST请求的详细信息,验证它是否在“标题”和“表单数据”下列出了
用户名的值?@Pickels是的,环顾四周后我发现connect已经是express的一个节点模块,所以我删除了所有这些。但是当我删除端口的配置行时,我找不到“获取”页面,因为它似乎不是默认的端口3000。@JonathanLonowski我已经用express和node版本以及一些标题信息更新了这个问题。谢谢大家看了这篇文章,我会把答案贴出来的,但我真的不太明白这一点,无法给出答案。ChromeREST客户端扩展似乎出了问题,它把事情搞砸了,然后影响了所有帖子,无论是使用firefox还是chrome。关闭所有内容并重新启动节点服务器后,使用我的php表单发布后,它工作了。经过测试,当我使用rest客户机时,情况会再次中断,但如果我再次关闭所有内容,则会正常工作。注意上面发布的响应标题的差异
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
Content-Type: application/x-www-form-urlencoded