router.param("postId", function(request, response, next, postId) { Post.findOne({_id: postId}, function(error, post) { if (error) return next(error); request.post = post; next(); }); });
// Preload user profile on routes with ':username' router.param('username', function(req, res, next, username){ User.findOne({username: username}).then(function(user){ if (!user) { return res.sendStatus(404); } req.profile = user; return next(); }).catch(next); });
models.param('modelId', (req, res, next, value) => { const model = data.models.find(m => m.id === (value * 1)) if(model){ req['model'] = model; next(); } else { res.status(404).send('Invalid model ID'); } })
router.param('id', function(req, res, next, id) { try { reqPath = id2path(id); } catch (err) { return handleError({ config, req, res })(Object.assign( err, { httpCode: 400 } )); } return next(); });
router.param("commentId", function(req, res, next, commentId) { req.comment = req.post.comments.id(commentId); if (!req.comment) return res.status(404).send(); // NOT FOUND next(); });
// Preload article objects on routes with ':article' router.param('article', function(req, res, next, slug) { Article.findOne({ slug: slug}) .populate('author') .then(function (article) { if (!article) { return res.sendStatus(404); } req.article = article; return next(); }).catch(next); });
routerOPDS_auth.param("urlEncoded", (req, _res, next, value, _name) => { (req as IRequestPayloadExtension).urlEncoded = value; next(); });
// validate id; lookup item in table router.param('id', function (req, res, next, id) { var item = _items[id]; if (item) { req.params.item = item; next(); } else { return res.send(404, 'NOT FOUND'); } });
// route middleware to validate :name adminRouter.param('name', function (req, res, next, name) { //do validation on name here //blah blah validation //log something so we know its working console.log('doing name validations on ' + name); // once validation is done save the item in the req req.name = name; //go to the next thing next(); });
router.param('comment', function(req, res, next, id) { Comment.findById(id).then(function(comment){ if(!comment) { return res.sendStatus(404); } req.comment = comment; return next(); }).catch(next); });
router.param("postId", function(req, res, next, postId) { Post.findById(postId, function(error, post) { if (error) return next(error); // error handling mid req.post = post; next(); }); });
routerOPDS_browse_v2.param("urlEncoded", (req, _res, next, value, _name) => { (req as IRequestPayloadExtension).urlEncoded = value; next(); });
routerOPDS_convert_v1_to_v2.param("urlEncoded", (req, _res, next, value, _name) => { (req as IRequestPayloadExtension).urlEncoded = value; next(); });
//Intercept route parameter router.param('userId', function(req, res, next, id) { console.log('userID callback invoked: ' + id); next(); });
router.param("postId", function(request, response, next, postId) { Post.findOne({_id: postId}, function(error, post) { if (error) return next(error); request.post = post; next(); }); });