Access app.set setting in controller of express application on node.js

In my app.js I set the following setting

app.set('mailTemplatesDir', __dirname + '/mails');

Now I would like to read the value of 'mailTemplatesDir' in one of my controllers, how can I access the setting there? I would prefer not to make app a global variable.

If you've created your app with the usual call to createServer() then there really is no other way to access option settings without going through the object returned by that function. Express does not cache the server object but simply returns the result of new on the object.

If you've created your application with the standard express boilerplate generated for you, you likely have a line creating app that looks like:

var app = module.exports = express.createServer();

This does not actually create app as a global variable but does make it available as a module export. You can access your mailTemplatesDir option from another module by requiring your app.js module this way:

var templateDir = require('./app').set('mailTemplatesDir');