req.body返回空对象(我已经包含bodyparser)

我是新来的后端JavaScript和我有一个表单提交应该显示提交的对象,但它显示一个空的对象,而不是。 我已经包括身体分析器,所以我不知道问题在哪里。 这是我的app.js文件:

const express = require("express"); const app = express(); const mustacheExpress = require("mustache-express"); const bodyParser = require("body-parser"); const pgp = require("pg-promise")(); app.engine("html", mustacheExpress()); app.set("view engine", "html"); app.set("views", __dirname + "/views"); app.use("/", express.static(__dirname + "/public")); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.get('/new_creature',function(req,res){ res.render('new_creature/index'); }); app.post('/new_creature', function(req,res){ res.end(JSON.stringify(req.body)); console.log(req.body); // res.render('new_creature/index'); }); 

这里是我的意见/生物/ new_creature / index.html文件:

 <form method="POST" action="/new_creature"> Species:<input type="text"> Family:<input type="text"> Habitat:<input type="text"> Diet:<input type="text"> Planet:<input type="text"> <input type="submit" placeholder="submit"> </form> 

如果你想bodyparser工作,你需要在你想要张贴的表单元素上有名字属性。 例如:

 <form method="POST" action="/new_creature"> Species:<input name='species' type="text"> Family:<input name='family' type="text"> Habitat:<input name='habitat' type="text"> Diet:<input name='diet' type="text"> Planet:<input name='planet' type="text"> <input type="submit" value="submit"> </form> 

另外,占位符在提交button上没有意义; 占位符是应该显示在未被填充的input中的幽灵文本。