app.use(bodyParser.json())有什么作用? [英] What does `app.use(bodyParser.json())` do?

查看:1189
本文介绍了app.use(bodyParser.json())有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对:

bodyParser.urlencoded({extended: ...})

我的研究表明,如果extended: true,则可以解析嵌套对象,或者通常是任何类型的对象.但是,如果设置extended: false,则只能解析字符串或数组.但是...

my research shows me that if extended: true, then you can parse nested objects, or generally any type. However, if you set extended: false, then you can only parse strings or arrays. But what does ...

app.use(bodyParser.json())

是什么意思?我的意思是,是的...我知道文档中提到它解析json.但是我仍然很困惑.我注意到设置extended: true的应用程序根本不使用bodyParser.json().但是extended: false的应用程序倾向于使用bodyParser.json().为什么是这样?最终,两个应用程序都可以解析json.

mean exactly? I mean, yes... I know the docs mention that it parses json. But I am still confused. I have noticed applications that set extended: true do NOT use bodyParser.json() at all. But applications that extended: false tend to use bodyParser.json(). Why is this? At the end of the day, both applications are able to parse json.

第二,推荐哪种方法?

推荐答案

好吧,与我以前的想法相反,进一步的研究表明extended: trueapp.use(bodyParser.json())可以一起使用.因此,使用它的不仅是extended: false.不管您将扩展名设置为true还是false,语句app.use(bodyParser.json())都可以独立使用.

Okay, contrary to what I previously thought, further research shows that extended: true and app.use(bodyParser.json()) can be used together. So it is not only extended: false that uses it. The statement app.use(bodyParser.json()) is to be used independently, whether you set extended as true or false.

  • app.use(bodyParser.json())基本上告诉系统您要使用json.

  • app.use(bodyParser.json()) basically tells the system that you want json to be used.

bodyParser.urlencoded({extended: ...})基本上告诉系统是要使用简单的算法来进行浅层解析(即,false)还是要使用复杂的算法来进行深层解析(可以对嵌套对象进行处理)(即true).

bodyParser.urlencoded({extended: ...}) basically tells the system whether you want to use a simple algorithm for shallow parsing (i.e. false) or complex algorithm for deep parsing that can deal with nested objects (i.e. true).

看看文档(例如 https://expressjs.com/en/例子中的guide/migrating-4.html ).

这篇关于app.use(bodyParser.json())有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆