node.js express 4.0 中的“扩展”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29960764/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:41:31  来源:igfitidea点击:

What does 'extended' mean in express 4.0?

node.jsexpressbody-parser

提问by KimCrab

I'm using express and also body-parser in my app.

我在我的应用程序中使用 express 和 body-parser。

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

But, What does 'extended' mean in express 4.0?

但是,“扩展”在 express 4.0 中是什么意思?

I found this

我找到了这个

extended - parse extended syntax with the qs module.

However, I still can't understrand what it means.

但是,我仍然无法理解这意味着什么。

回答by Wayne Chiu

If extendedis false, you can not post "nested object"

如果extendedfalse,则不能发布“嵌套对象”

person[name] = 'cw'

// Nested Object = { person: { name: cw } }

If extendedis true, you can do whatever way that you like.

如果extendedtrue,你可以做任何你喜欢的方式。

回答by Andrew Lam

When extendedproperty is set to true, the URL-encoded data will be parsed with the qs library.

extended属性设置为 时true,将使用qs 库解析 URL 编码的数据。

On the contrary,

相反,

when extendedproperty is set to false, the URL-encoded data will instead be parsed with the querystring library.

extended属性设置为 时false,URL 编码的数据将改为使用查询字符串库进行解析。



The differences between parsing with `qs library` vs `querystring library`

用`qs library` 和`querystring library` 解析的区别

  • qslibrary allows you to create a nestedobject from your query string.

    var qs = require("qs")
    var result = qs.parse("person[name]=bobby&person[age]=3")
    console.log(result) // { person: { name: 'bobby', age: '3' } }
    


  • query-stringlibrary does notsupport creating a nested object from your query string.

    var queryString = require("query-string")
    var result = queryString.parse("person[name]=bobby&person[age]=3")
    console.log(result) // { 'person[age]': '3', 'person[name]': 'bobby' }
    


  • qslibrary will notfilter out '?' from the query string.

    var qs = require("qs")
    var result = qs.parse("?a=b")
    console.log(result) // { '?a': 'b' }
    


  • query-stringlibrary will filter out '?' from the query string.

    var queryString = require("query-string")
    var result = queryString.parse("?a=b")
    console.log(result) // { a: 'b' }
    
  • qs库允许您从查询字符串创建嵌套对象。

    var qs = require("qs")
    var result = qs.parse("person[name]=bobby&person[age]=3")
    console.log(result) // { person: { name: 'bobby', age: '3' } }
    


  • 查询字符串支持从您的查询字符串创建嵌套对象。

    var queryString = require("query-string")
    var result = queryString.parse("person[name]=bobby&person[age]=3")
    console.log(result) // { 'person[age]': '3', 'person[name]': 'bobby' }
    


  • qs不会过滤掉'?' 从查询字符串。

    var qs = require("qs")
    var result = qs.parse("?a=b")
    console.log(result) // { '?a': 'b' }
    


  • 查询字符串库将过滤掉 '?' 从查询字符串。

    var queryString = require("query-string")
    var result = queryString.parse("?a=b")
    console.log(result) // { a: 'b' }
    


app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded


For more information, you may refer to Leonid Beschastny's answer, and npm compare qs vs query-string.


有关更多信息,您可以参考Leonid Beschastny 的回答npm compare qs vs query-string

回答by Logan Tegman

From the Body-Parser docs:

来自 Body-Parser 文档:

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).

包含解析数据的新主体对象填充在中间件之后的请求对象上(即 req.body)。该对象将包含键值对,其中值可以是字符串或数组(当扩展为假时)或任何类型(当扩展为真时)。

And

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The "extended" syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please see the qs library.

扩展选项允许在使用查询字符串库(当为假)或 qs 库(当为真)解析 URL 编码数据之间进行选择。“扩展”语法允许将丰富的对象和数组编码为 URL 编码格式,从而实现类似 JSON 的 URL 编码体验。有关更多信息,请参阅 qs 库。

Basically extended allows you to parse full objects.

基本上扩展允许您解析完整的对象。

Body Parser: Url Encoded

正文解析器:网址编码