node.js - 在 Express JS Api 中使用 Put 方法时,是否需要使用正文解析器?
问题描述
在下面的代码片段中,我是否需要像在 Post 方法中那样使用 urlencodedParser。
app.put('/api/provider/:id', urlencodedParser, function (req, res) {
}
解决方案
body-parser
将请求的主体解析为req.body
,您的put
中间件可能需要它。body-parser
现在内置于 Express 中(从v4.16.0 开始- 下面假设您有更新版本)。
最简单的实现是在所有请求中使用express.json
and express.urlencoded
(used in ) , using ,这样您就不必在中间件中担心它。以下是为您设置的方法:body-parser
app.use
npx express-generator $APP_NAME
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
注意:如果您期望在您的 requests 中有嵌套对象,则需要设置extended
为。true
推荐阅读
- android - Flutter:如何将非最终值传递给另一个类?
- elasticsearch - 弹性搜索未正确排序
- c# - 在 EF Core 3.1 中替换 Map() 的最佳方法是什么
- node.js - 忘记密码不会使用 Pug 路由到 api
- python - 如何在不丢失我的项目的情况下用 64 位替换 python 32 位?
- typescript - WebXR 打字稿支持
- python - replace('\n','') 不适用于一系列 \n\n\n
- java - 如何在 libSVM 中使用交叉验证?
- android - 获取设备 ID - Express
- machine-learning - 预期的二维数组,得到一维数组:拟合模型后