Set app.set parameter in express application controller 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 the app global variable.

+4
source share
1 answer

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

If you created your application with the standard express template template generated for you, you will likely have a line creating an app that looks like this:

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

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

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

Source: https://habr.com/ru/post/1412186/


All Articles