`app.use(bodyParser.json())` 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39870867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does `app.use(bodyParser.json())` do?
提问by Grateful
For:
为了:
bodyParser.urlencoded({extended: ...})
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 ...
我的研究表明,如果extended: true,那么您可以解析嵌套对象,或者通常可以解析任何类型。但是,如果设置extended: false,则只能解析字符串或数组。但是有什么...
app.use(bodyParser.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: truedo NOT use bodyParser.json()at all. But applications that extended: falsetend to use bodyParser.json(). Why is this? At the end of the day, both applications are able to parse json.
是什么意思?我的意思是,是的......我知道文档提到它解析 json。但我仍然很困惑。我注意到设置的应用程序extended: true根本不使用bodyParser.json()。但是extended: false倾向于使用bodyParser.json(). 为什么是这样?归根结底,这两个应用程序都能够解析 json。
Secondly, which is the recommended approach?
其次,推荐的方法是什么?
回答by Grateful
Okay, contrary to what I previously thought, further research shows that extended: trueand app.use(bodyParser.json())can be used together. So it is not only extended: falsethat uses it. The statement app.use(bodyParser.json())is to be used independently, whether you set extended as trueor false.
好了,相反的是我以前认为,进一步的研究显示,extended: true并且app.use(bodyParser.json())可以一起使用。所以它不仅仅是extended: false使用它。该语句app.use(bodyParser.json())将独立使用,无论您将扩展设置为true还是false。
app.use(bodyParser.json())basically tells the system that you want json to be used.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).
app.use(bodyParser.json())基本上告诉系统你想要使用 json。bodyParser.urlencoded({extended: ...})基本上告诉系统您是要使用简单算法进行浅层解析(即 false)还是使用复杂算法进行可处理嵌套对象的深度解析(即 true)。
Have a look at the docs (i.e. https://expressjs.com/en/guide/migrating-4.html) for the example.
回答by trincot
URL encoding and JSON encoding both allow to convert a (nested) object to string, but the format is different. An URL encoded string is in general not a valid JSON string.
URL 编码和 JSON 编码都允许将(嵌套)对象转换为字符串,但格式不同。URL 编码的字符串通常不是有效的 JSON 字符串。
One application may use one encoding method, and another the other. As long as they don't mix the two, it will work.
一个应用程序可以使用一种编码方法,而另一种使用另一种编码方法。只要他们不混合两者,它就会起作用。
回答by Malatesh Patil
bodyParser.json returns middleware that only parses json. This parser accepts any Unicode encoding of the body and supports automatic inflation of gzip and deflate encodings.
bodyParser.json 返回只解析 json 的中间件。此解析器接受正文的任何 Unicode 编码,并支持 gzip 和 deflate 编码的自动膨胀。
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body).
包含解析数据的新主体对象填充在中间件之后的请求对象上(即 req.body)。

