有 quite a list of Express.js middlewares替换内置函数。例如:
body-parser - Parse HTTP request body. Replaces built-in function express.bodyParser
当他们说“替换内置函数”时,他们是什么意思?这些替代品是否在内置版本之上提供了任何改进?我的意思是我看不出使用额外的模块有什么意义,如果他们不这样做的话。我问是因为我没有看到它在任何地方被提及,至少在中间件文档中是这样。
最佳答案
这意味着它会覆盖应用程序中 native 提供的默认行为或对象。
文档摘录,例如 res.json
:
This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.
...
A new body object containing the parsed data is populated on the request object after the middleware (i.e.
req.body
), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.
所讨论的正文解析器对我很有用,例如,我用它来恢复以 post 形式传输的数据。
自更新以表达v4 原始内置函数were separated into standalone modules .现在每个模块都是独立的,但仍作为依赖项包含在 package.json of express.js 中.
这有点令人困惑。
关于javascript - 中间件取代内置 Express.js 函数有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841684/