V
V
Vitaly2015-10-13 08:51:59
JavaScript
Vitaly, 2015-10-13 08:51:59

How to put the nodejs server settings into a separate file?

Hello.
Sorry for a possibly stupid question, but still tell a beginner how to do it right.
I have a nodejs express project, I plan to deploy it on several machines, and in order not to climb into the server code every time, I want to create a settings file (server ip, port ....,) how can I do it right?
P.S. : And how can you transfer data from this config to client-side JavaScript, from the public folder, connected to index.ejs ?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
T
Timur Shemsedinov, 2015-10-13
@MarcusAurelius

There is no right way, there are many different ones, all of them are wrong. I offer two of the wrong ways to choose from:
config.js file:

module.exports = {
  host: 'hostname',
  port: 80
};

server.js file:
// Подключение конфига
var config = require('./config.js');
// Использование конфига
console.dir(config);

config.json file:
{
  "host": "hostname",
  "port": 80
}

server.js file:
// Подключение конфига
var config = require('./config.json');
// Использование конфига
console.dir(config);

A
Alex, 2015-10-13
@streetflush

config.js

var config = {}

config.database = {};
config.server= {};

config.database.user = '';
config.database.password = '';
config.database.server = '';
config.database.database =
config.database.options = {
};
config.server.port = ;


module.exports = config;

server.js
var appConfig = require('./server/config/config');
...
app.set('port', appConfig.server.port || 8080);
...

S
Sergey, 2015-10-13
Protko @Fesor

In addition to the above, I suggest using ENV variables to store that part of the configs that depends on the environment (hosts, ports, passwords).

V
Vitaliy, 2015-10-13
@Scorpiored88

And how can you transfer data from this config to client-side JavaScript, from the public folder, connected to index.ejs ?

K
Konstantin Kitmanov, 2015-10-13
@k12th

I do like this: example and description . The meaning is about the same as in Django applications: config is code, import the module depending on some environment variable, ./local.js takes precedence over everything else.
Advantages: very flexible; you can "inherit" from each other and redefine configs in any combination; no need to learn another config language (preved yaml) and no need to put up with its shortcomings (preved jason).
Disadvantages: not declarative; there is no npm module, so out of the box; require caches connected modules, so you have to dance with a tambourine to update the config on the fly.

A
Alexander, 2016-02-26
@w4r_dr1v3r

Is the given syntax valid for the new fifth node? otherwise I’ll see everything becomes obsolete faster than shit coded by developers

A
a1en_yeah, 2016-10-03
@a1en_yeah

And if we want to modify the config from the client, how should it be connected? Really directly to load at each start of function it using? After all, require will load the file only once, at the start of the server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question