Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Created February 29, 2012 23:20
Show Gist options
  • Save devinrhode2/1945313 to your computer and use it in GitHub Desktop.
Save devinrhode2/1945313 to your computer and use it in GitHub Desktop.
File system based routing within express
In express app.js, remove all app.get routings, and put this:
app.get('*', function(req, res) {
file = req.params[0].substr(1, req.params[0].length);
console.log('requesting: ' + file);
res.render(file, {locals: {
request: req,
params: req.query
}}); //in your .jade file, variables request and params are available.
})
//In your view directory, create a new file, .html, .jade with the jade module, .ejs with the ejs module, and navigate to it in your web browser. This should 'just work'. Creating /newfolder/index.jade, will, you guessed it, be the page for /newfolder/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment