This is how I've designed my route and I'm not really happy with it. I'd rather be able to break down each hasValid
into its own middleware, but I don't see how that would work since it wouldn't stop execution.
const secretSender = async (req, res, next) => {
if (!hasValidA(req)) {
return next()
}
if (!hasValidB(req)) {
return next()
}
if (!hasValidC(req)) {
return next()
}
if (!hasValidD(req)) {
return next()
}
res.send('something secret')
}
router.use(secretSender)
router.get('*', (req, res) => {
res.send('something public')
})
It also doesn't really make sense to make the default route "something secret" and have "something public" be the middleware since "something public" is the default behavior.