javascript - 如何正确使用 Body-parser 中间件
问题描述
我是 node.js 的新手,我无法正确启动 body-parser 中间件。
如教程所述,我尝试将 bodyParsers 实例化为 const,然后尝试在 app.use 中使用它。
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const users = [
{name: 'Jones', email: 'jones@gmail.com'},
{name: 'Henrique', email: 'henrique@hotmail.com'}
]
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/users', (req,res)=> {
res.send(users);
})
app.post ('/users', (req,res)=>{
console.log(req.body);
res.end();
})
app.get('/', (req, res) => {
res.send('Rota padrão');
console.log();
});
// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});
我收到了这个错误:
解决方案
推荐阅读
- c# - IEnumerable
> 和扩展方法 - spring - Spring Cloud Vault - 缺少必需的标头:X-Config-Token
- refresh - Angular View 不会在数组推送时刷新
- jenkins - 未找到名为 SonarQube Scanner 2.8 的工具错误
- excel - 如果单元格未填充,则停止用户打印
- java - 重定向java spring boot上的cookie创建
- docker - E: 无法在多阶段 Docker 构建中找到包
- protractor - 如何在量角器配置文件中动态设置 multiCapabilities
- vue.js - 入口点 - webpack.config.js vs vue.config.js
- css - React - 单击时如何扩展元素并隐藏其他元素