无法使用 node.js v0.12.7、express 4、body-parser、bootstrap 3 检索 POST 数据
Can't retrieve POST data using node.js v0.12.7, express 4, body-parser, bootstrap 3
我在以前的应用程序中已经这样做了几十次,但由于某种原因无法让它继续工作。不知道是不是版本问题。我查看了我可以在网上找到的每个示例,并且 none 中的示例正在运行。不知道我做错了什么。相关代码:
index.html(从 bootstrap 站点复制 - 我只在表单标签中添加了角色、操作和方法属性)
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<form role="form" action="/form" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>
server.js
var express = require("express");
var app = express();
var bp = require("body-parser");
var path = require("path");
app.use(bp.urlencoded({extended: false}));
app.use(bp.json());
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
app.post("/form", function(req, res) {
console.log(req.body);
res.end();
});
var server = app.listen(4000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
package.json
{
"name": "test-app",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.13.3",
"express": "^4.13.3",
"sqlite3": "^3.0.10"
}
}
调用表单路由时,req.body
始终是 {}
。不过,同样的设置在过去每次都对我有用。
您的表单 input
元素缺少 name
属性,因此未包含在 POST 请求中。 See this answer.
我在以前的应用程序中已经这样做了几十次,但由于某种原因无法让它继续工作。不知道是不是版本问题。我查看了我可以在网上找到的每个示例,并且 none 中的示例正在运行。不知道我做错了什么。相关代码:
index.html(从 bootstrap 站点复制 - 我只在表单标签中添加了角色、操作和方法属性)
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<form role="form" action="/form" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>
server.js
var express = require("express");
var app = express();
var bp = require("body-parser");
var path = require("path");
app.use(bp.urlencoded({extended: false}));
app.use(bp.json());
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
app.post("/form", function(req, res) {
console.log(req.body);
res.end();
});
var server = app.listen(4000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
package.json
{
"name": "test-app",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.13.3",
"express": "^4.13.3",
"sqlite3": "^3.0.10"
}
}
调用表单路由时,req.body
始终是 {}
。不过,同样的设置在过去每次都对我有用。
您的表单 input
元素缺少 name
属性,因此未包含在 POST 请求中。 See this answer.