Skip to content

Instantly share code, notes, and snippets.

@A
Created November 30, 2013 14:21
Show Gist options
  • Save A/7719691 to your computer and use it in GitHub Desktop.
Save A/7719691 to your computer and use it in GitHub Desktop.
Routes also have the .param method to allow for easier resource loading in this case we will make sure all our routes load the post
// vendor
var express = require('express');
var router = new express.Router();
// routes also have the .param method to allow for easier resource loading
// in this case we will make sure all our routes load the post
router.param('post_id', function(req, res, next, post_id) {
// simulate loading a post
setTimeout(function() {
req.post = { name: 'foo' };
next();
}, 1000);
});
router.get('/:post_id', function(req, res, next) {
res.send(req.post.name + '\n');
});
router.post('/:post_id/comment', function(req, res, next) {
// we could add the comment to req.post here
res.send('\\o/\n');
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment