Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created February 8, 2012 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaskis/1767058 to your computer and use it in GitHub Desktop.
Save slaskis/1767058 to your computer and use it in GitHub Desktop.
Inherit some sweet sweet locals plz
var express = require('express');
function middleware(req,res,next){
// do real funky stuff here...
// add access to funky stuff in routes
req.funky = function(){return 'funky stuff'}
// add access to funky stuff in views
res.locals.funky = function(){return 'funky stuff'}
// move along
next()
}
// inline template for test
function engine(path,opts,fn){
require('jade').render('p=funky()',opts,fn);
}
var sub = express();
sub.engine('jade',engine);
sub.get('/',function(req,res,next){
res.send(req.funky()) // req.funky() works!
})
sub.get('/locals',function(req,res,next){
res.render('locals.jade') // ReferenceError: funky is not defined :,(
})
var app = express();
app.engine('jade',engine);
app.use(middleware)
app.use('/sub',sub);
app.get('/',function(req,res,next){
res.send(req.funky())
})
app.get('/locals',function(req,res,next){
res.render('locals.jade') // funky() works!
})
app.listen(1234)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment