41

This may not me be the right approach, but I want to conditionally add an object/parameter to the app variable inside of an expressJS/connectjS middleware call.

Since this function is a callback, what's the standard/best way to access app from inside a middleware call?

  //app.js
  var myMiddleware = require('./lib/mymiddleware.js');
  ...
  app.configure( function(){
    app.use( myMiddleware.func() );
    ...
  }

  if( 'object' !== typeof app.myObject ){
    cry( 'about it' );
  } 


  //mymiddleware.js
  module.exports.func = function( ){
    return function( req, res, next ){
       //append app object
       //app.myObject = {}
       next();
    }
  };

Note, this is not something for locals or settings to later be rendered, but something that will be used in routes and sockets later down the execution chain.

qodeninja
  • 10,946
  • 30
  • 98
  • 152

3 Answers3

115

Request objects have an app field. Simply use req.app to access the app variable.

Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54
10

Generally I do the following.

var myMiddleware = require('./lib/mymiddleware.js')(app);
...
app.configure( function(){
  app.use( myMiddleware );
  ...
}

And the middleware would look like this...

module.exports = function(app) {
  app.doStuff.blah()

  return function(req, res, next) {
    // actual middleware
  }
}
Morgan ARR Allen
  • 10,556
  • 3
  • 35
  • 33
  • 1
    I do it the same as you, I like the `req.app` solution though. wasn't aware that was possible. – Plato Sep 19 '13 at 01:56
  • 2
    @Plato the problem with this that I just realized is that this only updates the app object on a page request, I need the app object to be updated as part of the startup. – qodeninja Sep 23 '13 at 23:10
  • Is there another way to get app into the function without passing it in the require function? – qodeninja Sep 23 '13 at 23:11
  • @qodeninja: if that is your new requirement, you should edit the question. Regardless, I am unclear that "at app startup" means: there is no `req` outside of a request (that is: at startup), and you specifically said "middleware", which implies a request, so my original answer should be ok. Or am I missing something? – Nitzan Shaked Sep 24 '13 at 03:28
  • @NitzanShaked I didnt really get how middleware worked before, now I understand that middleware only runs on page loads, rather than startup. I need to update the app object in the startup phase, to set global properties. Though, I was hoping there was a more standard way of doing that. When I actually tested your code I realized it was not what I had intended, my apologies. Though I think its pretty clear from the code I wrote what I was trying to do `typeof app.myObject` for example. The requirement didnt change, my understand of the problem did. – qodeninja Sep 24 '13 at 18:08
4

You can also attach a variable to the Node global object, like so:

//some-module.js
global.someVariable = "some value!";

//another-module.js
console.log(global.someVariable); // => "some value!"

Note that Nitzan Shaked's answer (using req.app) is a much better approach.

In fact, I don't advise using this solution at all. I'm leaving it solely for completeness, because it works, but it's bad practice in almost every situation (excepting adding polyfills).

Andrew Faulkner
  • 3,662
  • 3
  • 21
  • 24