Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  164] [ 5]  / answers: 1 / hits: 73208  / 10 Years ago, wed, march 12, 2014, 12:00:00

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?




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


More From » node.js

 Answers
1

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
};

[#72035] Monday, March 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
vanessag

Total Points: 170
Total Questions: 98
Total Answers: 88

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;