Node.js / Express表单post req.body不工作 [英] Node.js/Express form post req.body not working
问题描述
我正在使用express并且无法从bodyParser获取表单数据。无论我做什么,总是作为一个空的对象出现。这是我的快速生成的app.js代码(我唯一添加的是最下面的app.post路由):
var express = require('express');
var app = module.exports = express.createServer();
//配置
app.configure(function(){
app.set('views',__dirname +'/ views');
app.set('view engine','jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
应用程序.use(app.router);
app.use(express.static(__ dirname +'/ public'));
});
app.configure('development',function(){
app.use(express.errorHandler({dumpExceptions:true,showStack:true}));
}) ;
app.configure('production',function(){
app.use(express.errorHandler());
});
//路由
app.get('/',function(req,res){
res.sendfile('./ public / index.html ');
});
app.post('/',function(req,res){
console.log(req.body);
res.sendfile('./ public / index .html');
});
app.listen(3010);
这是我的HTML表单:
<!doctype html>
< html>
< body>
< form id =myformaction =/method =postenctype =application / x-www-form-urlencoded>
< input type =textid =mytext/>
< input type =submitid =mysubmit/>
< / form>
< / body>
< / html>
当我提交表单时,req.body是一个空对象{}
值得注意的是,即使我从表单标签中删除了enctype属性,也会发生这种情况。
...有没有我缺少的东西/做错了?
我正在使用节点v0.4.11和表达v2.4.6
< form id =myformaction =/method =postenctype =application / x-www-form-urlencoded>
< input type =textname =I_appear_in_req_bodyid =mytext/>
< input type =submitid =mysubmit/>
< / form>
HTTP文章的正文是所有表单控件的键/值哈希值,带有 name
属性,值为控件的值。
您需要为所有输入添加名称。 / p>
I'm using express and having trouble getting form data from the bodyParser. No matter what I do it always comes up as an empty object. Here is my express generated app.js code (the only thing I added was the app.post route at the bottom):
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.sendfile('./public/index.html');
});
app.post('/', function(req, res){
console.log(req.body);
res.sendfile('./public/index.html');
});
app.listen(3010);
Here is my HTML form:
<!doctype html>
<html>
<body>
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" id="mytext" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>
When I submit the form, req.body is an empty object {}
Its worth noting that this happens even if I remove the enctype attribute from the form tag
...Is there something I am missing/doing wrong?
I am using node v0.4.11 and express v2.4.6
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="I_appear_in_req_body" id="mytext" />
<input type="submit" id="mysubmit" />
</form>
The body of a HTTP post is a key/value hash of all the form controls with a name
attribute, and the value is the value of the control.
You need to give names to all your inputs.
这篇关于Node.js / Express表单post req.body不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!