2014-11-05 128 views
0

我已经设置如下一个简单的HTML表单:无法从HTML表单数据发送到节点服务器

<!doctype html> 
<html> 
    <head> 
     <title>Testing Echo server</title> 
    </head> 
    <body> 
     <form method="post" action="/"> 
      <input type="text" id="keyword" name="keyword" /> 
      <input type="submit" value="echo" id="submit" /> 
     </form> 
    </body> 
</html> 

我app.js看起来是这样的:

var express = require('express'); 
var app = express(); 

app.get('/', function (req, res) { 
    res.sendFile(__dirname + '/index.html'); 
}); 

app.post('/', function (req, res) { 

     console.log(req.params); // logged as {} 
     res.writeHead(200); 
     //req.pipe(res);  // throws error 
     res.write('abc');  // works 
     res.end(); 

}); 

app.listen(8080); 

我无法访问参数从表单发送。

我该如何解决这个问题?

+0

该路由本身不包含任何参数,所以如何在cosole中预期参数。如果你试图获得表单数据,你应该使用req.body – Amitesh 2014-11-05 09:47:35

+0

'req.body'给我'undefined'。 – 2014-11-05 09:51:21

+0

您需要安装类似body-parser的解析器模块(https://github.com/expressjs/body-parser)。为了使您的开发更容易,请安装节点检查器来调试节点服务器。 – Amitesh 2014-11-05 10:00:28

回答

1

首先,如果你不想自己从http头中取出数据,你需要body-parser middleware

那么你不需要访问参数,因为你没有任何,但请求的主体与req.body

相关问题