Per-server NodeJS configuration

Problem

I have our first NodeJS server that’s being deployed to a client server along with the rest of the application. I have a few items that need to be configured on a server-specific basis but I’m unable to find any easy way to handle this.

On googling I’ve found a few choices — using a framework that has support built in (too late), setting up a configuration file that can be loaded (would work, but I don’t want to have to worry about keep one config file for each server we have and keeping those out of git).

I’d love to just have node determine what the request domain is (dev.ourdomain vs www.ourdomain) and just do a condition. It sounds easy, it likely IS easy, but I’m having trouble finding any way to determine that domain data.

Problem courtesy of: helion3

Solution

As @drachenstern mentioned, you could use request.headers.host, as in:

# get the path portion of the URI without optional port
var domain=request.headers.host.replace(/\:\d+$/,'');

but this wouldn’t provide a canonical domain if the request was made using an IP address rather than the server’s name.

A better option might be to use the hostname of the server, as in:

var domain=process.env[
process.env['OS'] && process.env['OS'].match(/^Win/) ? 'HOSTNAME' : 'HOST'
];

Solution courtesy of: Rob Raisch

View additional discussion.