在 Node.Js/Express 应用程序中存储数据库配置的最佳方式 [英] Best way to store DB config in Node.Js / Express app

查看:22
本文介绍了在 Node.Js/Express 应用程序中存储数据库配置的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 node.js/Express 上运行的开源应用程序中存储数据库配置(用户名、密码)的最佳方式是什么?两个具体问题:

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. 例如,我是否应该将它放在 /lib 文件夹中的一个单独的 config.js 文件中,并且永远不要将它包含在 GitHub 上公开可用的主存储库中?

  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?

要包含配置,是否像从需要它的文件中require('./config.js') 一样简单,或者有更好的方法吗?

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 如果问题看起来有点简单或表述得不太好,我很抱歉,但我才刚刚开始 :)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆