在Node.Js / Express应用程序中存储数据库配置的最佳方式 [英] Best way to store DB config in Node.Js / Express app
问题描述
在node.js / Express上运行的开源应用程序中存储DB配置(用户名,密码)的最佳方式是什么?两个具体的问题:
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:
-
我应该把它放在一个单独的config.js文件中的
/ lib
文件夹,例如,不要将其包含在GitHub上公开的主存储库中?
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?
要嵌入配置,是
PS抱歉,如果问题看起来有点简单或不那么好,但我刚刚开始:)
PS sorry if the questions seem a bit simple or not so well formulated, but I'm just starting :)
推荐答案
不知道这是否是最佳做法,但是个人我有一个 config.json
我存储我的数据库连接信息。然后我执行以下操作:
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
};
这篇关于在Node.Js / Express应用程序中存储数据库配置的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!