【发布时间】:2020-06-23 14:44:45
【问题描述】:
我读了一篇文章,让我相信app.use(express.json()) 是app.use(bodyParser.urlencoded({extended: false})) 的一个方便替代品,因为它会解析请求的主体并将其作为对象返回。但是由于sn-p中的一些奇怪原因:
app.use(express.json());
app.use('/add-product',(_req, res, _next) => {
res.send('<form action="/product" method="POST" ><input type="text" name="title"><button
type="submit">Add Product</button>');
});
app.use('/product', (req, res, _next) => {
console.log(req.body);
res.redirect('/');
});
第二个中间件中的console.log(req.body) 返回一个空对象,而使用app.use(bodyParser.urlencoded({extended: false}) 返回一个具有通过表单发送的正确值的对象。我发现很难理解,我做错了什么。任何好的解释将不胜感激。谢谢
【问题讨论】:
标签: node.js json express middleware body-parser