【发布时间】:2021-11-30 02:43:05
【问题描述】:
我正在尝试将不同的中间件(此处为 multer 和 auth0)组合在一起以降低代码复杂性。我可以编写一个返回中间件或仅返回 next() 的中间件(如果环境/参数不需要执行)。
类似这样的:
const constraints = {
'local': {
'GET': {},
'POST': {
'skills': {
secured: true,
upload: true,
geolocation: false,
illustrations: true,
schema: skillsSchema
},
'donations': {
secured: true,
upload: true,
geolocation: true,
illustrations: false,
schema: donationsSchema
}
}
},
}
接下来我应该返回吗?或下一个()?还是真的
function middleware(req, res, next) {
const section = req.params[0]
const method = req.method
const { body } = req
const {
secured,
upload,
geolocation,
illustrations,
schema
} = constraints[process.env.NODE_ENV][method][section];
// Should I return next ? or next() ? or true
const uploading = !upload ? next() : ops.multer
const securing = !secured ? next() : ops.auth0
const geoValid = !geolocation ? true : new PipeLine({
point: {
lat: body.lat,
lng: body.lng
}
})
.isPointInsidePolygon(coordinates)
const undrawValid = !illustrations ? true : new PipeLine({
body: body
}).undrawSplit().joiValidate(schema).undrawJoin()
res.locals.magicPipeLine = { geoValid, undrawValid }
return [uploading, securing]
}
路线是:
router.post(/^\/(donations|skills|blogs)/,
middleware,
async (req, res, next) => {
【问题讨论】: