What's the advantage of using top-level generic parsing:
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
instead of route-specific parsing:
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /newUser gets urlencoded bodies
app.post('/newUser', urlencodedParser, function (req, res) {
if (!req.body) {
return res.sendStatus(400)
} else {
...
}
})
According to the body-parser readme it says the route-specific is the most recommended way. Why? Why use it instead of top-level generic parsing?
原因是无条件执行中间件意味着当请求已经不满足任何前提条件(例如匹配路由或满足身份验证要求)时,您可能正在执行不必要的工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.