在Express JS Api中使用Put方法时,是否需要使用正文解析器?

时间:2019-03-18 21:47:06

标签: node.js express put

在下面的代码段中,是否需要像在Post方法中一样使用urlencodedParser。

    <handlers>
  <remove name="svc-ISAPI-4.0_64bit" />
  <remove name="svc-ISAPI-4.0_32bit" />
  <remove name="svc-Integrated-4.0" />
 </handlers>

1 个答案:

答案 0 :(得分:2)

body-parser将请求的正文解析为req.body,您可能需要put中间件。 body-parser现在内置于Express中(自v4.16.0起-以下假设您具有更新版本)。

最简单的实现是在所有请求中使用express.json使用express.urlencodedbody-parser(在app.use中使用),这样您就不必担心它在您的中间件中。 npx express-generator $APP_NAME会为您设置它的方式:

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

注意:您需要将extended设置为true if you are expecting nested objects in your requests