Created
February 23, 2012 01:59
-
-
Save davidkrisch/1889164 to your computer and use it in GitHub Desktop.
Express Route Param Pre-conditions combined with express-namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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