deemeetree deemeetree - 6 months ago 33
Node.js Question

Best way to store DB config in Node.Js / Express app

What would be the best way to store DB config (username, password) in an open source app that runs on Node.Js / Express? Two specific questions:

1) Shall I put it into a separate config.js file in

/lib
folder, for example, and never include it into the master repository that is publicly available on GitHub?

2) To inlcude the config, is it as simple as
require('./config.js')
from the file that needs it or is there a better way of doing it?

Thank you!

PS sorry if the questions seem a bit simple or not so well formulated, but I'm just starting :)

Answer

Not sure whether this is the best practice, but personally I have a config.json file where I store my db connection information. Then I do the following:

// options.js
var fs = require('fs'),
configPath = './config.json';
var parsed = JSON.parse(fs.readFileSync(configPath, 'UTF-8'));
exports.storageConfig=  parsed;

Then from a different file I do the following:

var options = require('./options');

var loginData = {
        host: options.storageConfig.HOST,
        user: options.storageConfig.user,
        password: options.storageConfig.password
};
Comments