Created
May 29, 2012 15:11
-
-
Save ovaillancourt/2828961 to your computer and use it in GitHub Desktop.
registering methods on app in a middleware
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
//That's your middleware code | |
function myMiddleware(app){ | |
var myVar = 42; //Put middleware-wide vars in the closure. | |
var actualMiddleware = function(req, res, next){ | |
//Do stuffs here | |
next(); | |
} | |
var UtilityMethods = { | |
method1 : function(){ | |
return myVar; | |
}, | |
method2 : function(){ | |
return "John"; | |
} | |
} | |
//That's when you make your stuffs accessible from the app object. | |
app.MySuperNamespace = UtilityMethods; | |
//And that's the middleware that will run on all your paths. | |
return actualMiddleware; | |
} | |
app.use(myMiddleware(app)); | |
app.MySuperNamespace.method1(); //<- that will return 42... | |
app.MySuperNamespace.method2(); //<- that will return "John"... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment