req.body就是{}。所有表单数据都不在req.body中。请注意,它在邮递员中很有用。
以下是服务器代码:
const _ = require('lodash');
const express = require('express');
const hbs = require('hbs');
const bodyParser = require('body-parser');
const {ObjectID} = require('mongodb');
const {mongoose} = require('./db/mongoose');
var {authenticate, authenticateAdmin} = require('./middleware/authenticate');
const port = process.env.PORT;
var app = express();
app.use(bodyParser.json());
app.set('view engine', 'hbs');
app.post('/users', (req, res) => {
var body = _.pick(req.body, ['email', 'password']);
console.log(body)
});
app.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
以下是相关的html部分:
<form action="/users" method="post">
<input type="email" name="email">
<input type="password" name="password">
<button type="submit">Done</button>
</form>
谢谢!
答案 0 :(得分:2)
将此行添加到服务器并运行
PhotoModel
答案 1 :(得分:2)
app.use(bodyParser.urlencoded({extended:true}))
您需要为编码正文添加此行。