Skip to content

Instantly share code, notes, and snippets.

@davidkrisch
Created February 23, 2012 01:59
Show Gist options
  • Save davidkrisch/1889164 to your computer and use it in GitHub Desktop.
Save davidkrisch/1889164 to your computer and use it in GitHub Desktop.
Express Route Param Pre-conditions combined with express-namespace
// This is an example of the Route Param Pre-condition
// feature of Express. It also uses express-namespace
// Documentation for the Route Param Pre-condition can
// be found here:
// http://expressjs.com/guide.html#route-param pre-conditions
var express = require('express'),
ns = require('express-namespace'),
app = express.createServer();
app.param('id', function(req, res, next, id) {
process.stdout.write('id = ' + id + '\n');
// Put the id in the request so it can be used by
// the route handler. Something more useful could
// be done here like loading configuration or
// pulling something out of the database
req.someId = id;
next();
});
app.namespace('/:id', function() {
app.get('/', function(req, res) {
res.send('hello world: ' + req.someId);
});
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment