Skip to content

Instantly share code, notes, and snippets.

@ovaillancourt
Created May 29, 2012 15:11
Show Gist options
  • Save ovaillancourt/2828961 to your computer and use it in GitHub Desktop.
Save ovaillancourt/2828961 to your computer and use it in GitHub Desktop.
registering methods on app in a middleware
//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