[英]Node.js request.body undefined
This is my server.js file: 这是我的server.js文件:
var express = require("express");
var bodyParser = require("body-parser");
var controllers = require('./controllers');
var app = express();
controllers.init(app);
app.set('view engine', 'vash');
app.use(bodyParser());
app.use(express.static(__dirname + "/public"));
app.listen(5000);
This is my index.js code in controllers folder: 这是我在controllers文件夹中的index.js代码:
(function(controllers){
var homeController = require('./homeController');
controllers.init = function(app){
homeController.init(app);
};
})(module.exports);
This is my homeController.js code: 这是我的homeController.js代码:
(function(homeController){
homeController.init = function(app){
app.get('/', function(req, res){
res.render('index', {title: "The Board"});
});
app.post("/", function(req, res){
console.log(req.headers);
console.log(req.body);
});
};
})(module.exports);
And this is my HTML: 这是我的HTML:
@html.extend('layout', function(model){
@html.block('body', function(model){
@if(model.error){
<p class="text-error">Error occurred: @model.error</p>
}
<div class="row">
<form action="/" method="post">Enter your name:
<input type="text" name="userName" placeholder="..." />
<br>
<button type="submit">Submit</button>
</form>
</div>
})
})
When I make the post action req.body is undefined. 当我进行后期操作时,req.body是未定义的。 How can I solve this problem?
我怎么解决这个问题?
Move app.use(bodyParser());
移动
app.use(bodyParser());
before route declaration controllers.init(app);
在路由声明
controllers.init(app);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.