Created
February 8, 2012 09:17
-
-
Save slaskis/1767058 to your computer and use it in GitHub Desktop.
Inherit some sweet sweet locals plz
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
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