helion3
helion3

Reputation: 37381

Per-server NodeJS configuration

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.

Upvotes: 1

Views: 444

Answers (3)

Discipol
Discipol

Reputation: 3157

why don't you just hardcode that information in a init.js and change it for each server? How often are you going to move the servers to need to do this? Just do this

init.js

module.exports = { domain: "dev.ourdomain"};

main.js

var domain = require( "init.js" ).domain;

I assume you are developing it on dev.ourdomain and dumping it on www.ourdomain. Just ignore dumping init.js so that the server's init version remains ^_^ This is what I do and it saves me the need to bloat the project with another module just for one command.

Hope this helps others who encounter this situation.

Upvotes: 0

Rob Raisch
Rob Raisch

Reputation: 17357

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'
];

Upvotes: 1

jcolebrand
jcolebrand

Reputation: 16025

You might consider if request.host has the data you need. It most likely would.

Upvotes: 0

Related Questions