在 Express JS Api 中使用 Put 方法时,是否需要使用 body 解析器?
When using Put Method in a Express JS Api , Do I need to use body parser?
在下面的代码片段中,我是否需要像在 Post 方法中那样使用 urlencodedParser。
app.put('/api/provider/:id', urlencodedParser, function (req, res) {
}
body-parser
parses the body of the request into req.body
, which you'll likely need for your put
middleware. body-parser
now comes built into Express (as of v4.16.0 - 下面假定您有更新版本)。
最简单的实现是在所有请求中使用express.json
和express.urlencoded
(在body-parser
中使用),使用app.use
,这样你就不必在你的中间件中担心它。以下是 npx express-generator $APP_NAME
将为您设置的方式:
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
注意:您需要将 extended
设置为 true
。
在下面的代码片段中,我是否需要像在 Post 方法中那样使用 urlencodedParser。
app.put('/api/provider/:id', urlencodedParser, function (req, res) {
}
body-parser
parses the body of the request into req.body
, which you'll likely need for your put
middleware. body-parser
now comes built into Express (as of v4.16.0 - 下面假定您有更新版本)。
最简单的实现是在所有请求中使用express.json
和express.urlencoded
(在body-parser
中使用),使用app.use
,这样你就不必在你的中间件中担心它。以下是 npx express-generator $APP_NAME
将为您设置的方式:
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
注意:您需要将 extended
设置为 true