【问题标题】:Do body-parser json() and urlencoded() functions replace express json() and urlencoded() functions?body-parser json() 和 urlencoded() 函数是否替换了 express json() 和 urlencoded() 函数?
【发布时间】:2019-07-06 16:53:39
【问题描述】:

express 框架带有 express 函数express.json()express.urlencoded()

我们有 body-parser 库,它似乎执行相同的功能。

我的问题是,body-parser 是否替换了 express.json()express.urlencoded()

它处理创建req.body 的方式是否不同于内置的 Express 函数?

【问题讨论】:

    标签: javascript node.js rest express


    【解决方案1】:

    在 Express 4.0 之前,body-parser 包作为 Express 中间件存在。那时它已从 Express 捆绑包中删除,但人们仍然依赖它。所以大约 4.1 Express 把它带回来了,但把它集成到了框架中。

    所以是的,它实际上是一个直接的替代品,因为这些函数直接基于 body-parser。因此,它们本质上是同义词。 Express 团队实际上维护了 body-parser。

    【讨论】:

    • 谢谢@bob 我很欣赏你的重播。如果我在 express.json() 和 express.urlencoded() 上使用 body-parser,它在功能上会有所不同吗?
    • 不,它在功能上是一样的。您的请求将按照您的习惯构建。
    • 感谢 Bob,你是为我回答这个问题的冠军。
    • 查看 express 源代码,我发现 express.json 和 express.urlencoded 实际上只是 body-parser 的包装器。所以无论你使用 body parser 还是 express 版本,它都是相同的基本源代码。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2015-08-16
    • 1970-01-01
    • 2020-10-06
    • 2020-03-13
    相关资源
    最近更新 更多