Express: How can I read a value from the configuration

I am a NodeJS newbie and have a really simple question.

I want to store the port of the server in the application configuration. I use something like this to store it in the app config:

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());

  // My configuration:
  app.set('port', 80);
});

But how can I use the value of port? I cannot find an answer in the Express documentation.

Something like console.log('Port: ' + app.get('port')); doesn't work (TypeError: Array.prototype.toString is not generic).

Is there a better/other way to store a simple config value with the express framework?

Answers


If you use Express 2.x.x then take a look at the definition of the function set(key, value): https://github.com/visionmedia/express/blob/2.5.11/lib/http.js#L436

Line 444: this.settings[setting] = val;

So this should works:

console.log(app.setting['port']);

Even simpler: app.set(setting) with only one argument returns the value of setting. This has the advantage of not using implementation-dependent features, and will work in 3.x.x as well


Need Your Help

An Efficient way to store table ID in form so user cannot overwrite another table record

php security encryption spoofing

I am creating a website which has user accounts. For each user account, the user can do stuff like update their personal details, write a blog etc.

Possible compiler bug: Weird results using boost bessel functions with Intel compiler between two machines?

c++ linux boost intel

I'm trying to use boost's bessel function (cyl_bessel_j) in a project. However, I'm finding that the function is returning results with an incorrect sign after around 2000 calls to it.