Express 4.x API provides an example for utilizing Router.param
:
router.param('user', function (req, res, next, id) {
// Attempt to retrieve user details from the User model and attach it to the request object
User.find(id, function (err, user) {
if (err) {
next(err)
} else if (user) {
req.user = user
next()
} else {
next(new Error('Failed to load user'))
}
})
})
What is the most effective approach to make this work with TypeScript? The statement req.user = user
will not function as expected in this scenario, as the express.Request
object does not contain a user
property.
Once that hurdle is overcome, how can you implement it in a subsequent router.get
call?