在节点中存储DB配置的最佳方式。Js /表达应用程序

时间:2021-05-02 20:17:37

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:

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

  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?

    我要把它放在一个单独的配置中吗?例如,在/lib文件夹中的js文件,并且从来没有将它包含到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?

    要对配置进行inlcude,它是像需要它的文件中的require('./config.js')一样简单,还是有更好的方法来实现它?

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

PS不好意思,如果问题看起来有点简单或者没有很好地表述,但是我只是开始)

6 个解决方案

#1


15  

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:

我不确定这是否是最佳实践,但就我个人而言,我有一个配置。我存储db连接信息的json文件。然后我做以下工作:

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

#2


68  

Here's how I do it:

我是这么做的:

Create a config.js which contains objects representing your configs:

创建一个配置。包含代表您的configs的对象的js:

var config = {
development: {
    //url to be used in link generation
    url: 'http://my.site.com',
    //mongodb connection settings
    database: {
        host:   '127.0.0.1',
        port:   '27017',
        db:     'site_dev'
    },
    //server details
    server: {
        host: '127.0.0.1',
        port: '3422'
    }
},
production: {
    //url to be used in link generation
    url: 'http://my.site.com',
    //mongodb connection settings
    database: {
        host: '127.0.0.1',
        port: '27017',
        db:     'site'
    },
    //server details
    server: {
        host:   '127.0.0.1',
        port:   '3421'
    }
}
};
module.exports = config;

Then in my index.js (or wherever really),

然后在我的索引。js(或者无论真的),

var env = process.env.NODE_ENV || 'development';
var config = require('./config')[env];

Then process with that object, e.g.

然后处理那个对象,例如。

var server = express();
server.listen(config.server.port);
...

#3


5  

For running toy apps where I need to hide db credentials, I use the dotenv module.

对于运行需要隐藏db凭据的玩具应用程序,我使用dotenv模块。

Place your sensitive info in a .env file (which is .gitignored), place require('dotenv').config(); in your app; dotenv creates entries in process.env that you can refer to.

将您的敏感信息放在.env文件(即.gitignored)中,放置require('dotenv').config();在你的应用程序;dotenv在进程中创建条目。你可以参考。

.env file:

.env文件:

DATABASE_PASSWORD=mypw
DATABASE_NAME=some_db

To refer to the values:

参考价值:

process.env.DATABASE_PASSWORD

#4


3  

I do put in args. just like the port of so many node.js example. you most likely forever, pm2, nodemon to run your app. so this variable is not check in as part of your source code. and they are globally available too.

我放args。就像很多节点的端口。js的例子。您很可能永远,pm2, nodemon运行您的应用程序。所以这个变量不是作为源代码的一部分来检查的。它们也可以在全球范围内使用。

process.env.PORT
process.env.DATABASE_USER
process.env.DATABASE_PASSWORD


PORT=3000 DATABASE_HOST=localhost DATABASE_USER=admin DATABASE_PASSWORD=mypassword node app.js

export PORT=3000
export DATABASE_HOST=localhost
export DATABASE_PORT=27017
export DATABASE_USER=admin
export DATABASE_PASSWORD=mypassword
node app.js

var server = app.listen(process.env.PORT, function() {
});

var mongoClient = new MongoClient(new Server(process.env.DATABASE_HOST, process.env.DATABASE_PORT));

#5


1  

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?

要对配置进行inlcude,它是像需要它的文件中的require('./config.js')一样简单,还是有更好的方法来实现它?

This is the right way to store config files.

这是存储配置文件的正确方法。

The best approach would be to write your entire application like an ordinary node.js module, and write a small start-up file that calls it. This idea also allow you to use different database drivers using dependency injection.

最好的方法是像普通节点一样编写整个应用程序。并编写一个调用它的小型启动文件。这个想法还允许您使用依赖注入来使用不同的数据库驱动程序。

Good, but not perfect solution is the environment. It is shared among all application, so if you have certain data you want to be available to all of them, this is the best bet. But if you have a config for one particular app, not much so.

好的,但不是完美的解决方案是环境。它是在所有应用程序之间共享的,所以如果您有一定的数据,您希望对所有应用程序都可用,这是最好的选择。但是,如果你有一个特定应用的配置,那就没那么重要了。

PS: And please, don't use JSON for this. It's the worst idea possible. :)

PS:请不要使用JSON。这是最坏的想法。:)

#6


1  

I found this a nice way to handle my config, considering different environments:

我发现这是处理配置的好方法,考虑到不同的环境:

config.coffee

config.coffee

exports.setEnvironment = (env) ->
    switch env
        when "development"
            exports.DEBUG_LOG = true
            exports.DB_PORT = '27017'
            # ...
        when "testing"
            exports.DEBUG_ERROR = true
            exports.DEBUG_CLIENT = true
            # ...
        when "production"
            exports.DEBUG_LOG = false
            # ...
        else console.log "environment #{env} not found"

server.coffee:

server.coffee:

config = require('./config')
config.setEnvironment env

#1


15  

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:

我不确定这是否是最佳实践,但就我个人而言,我有一个配置。我存储db连接信息的json文件。然后我做以下工作:

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

#2


68  

Here's how I do it:

我是这么做的:

Create a config.js which contains objects representing your configs:

创建一个配置。包含代表您的configs的对象的js:

var config = {
development: {
    //url to be used in link generation
    url: 'http://my.site.com',
    //mongodb connection settings
    database: {
        host:   '127.0.0.1',
        port:   '27017',
        db:     'site_dev'
    },
    //server details
    server: {
        host: '127.0.0.1',
        port: '3422'
    }
},
production: {
    //url to be used in link generation
    url: 'http://my.site.com',
    //mongodb connection settings
    database: {
        host: '127.0.0.1',
        port: '27017',
        db:     'site'
    },
    //server details
    server: {
        host:   '127.0.0.1',
        port:   '3421'
    }
}
};
module.exports = config;

Then in my index.js (or wherever really),

然后在我的索引。js(或者无论真的),

var env = process.env.NODE_ENV || 'development';
var config = require('./config')[env];

Then process with that object, e.g.

然后处理那个对象,例如。

var server = express();
server.listen(config.server.port);
...

#3


5  

For running toy apps where I need to hide db credentials, I use the dotenv module.

对于运行需要隐藏db凭据的玩具应用程序,我使用dotenv模块。

Place your sensitive info in a .env file (which is .gitignored), place require('dotenv').config(); in your app; dotenv creates entries in process.env that you can refer to.

将您的敏感信息放在.env文件(即.gitignored)中,放置require('dotenv').config();在你的应用程序;dotenv在进程中创建条目。你可以参考。

.env file:

.env文件:

DATABASE_PASSWORD=mypw
DATABASE_NAME=some_db

To refer to the values:

参考价值:

process.env.DATABASE_PASSWORD

#4


3  

I do put in args. just like the port of so many node.js example. you most likely forever, pm2, nodemon to run your app. so this variable is not check in as part of your source code. and they are globally available too.

我放args。就像很多节点的端口。js的例子。您很可能永远,pm2, nodemon运行您的应用程序。所以这个变量不是作为源代码的一部分来检查的。它们也可以在全球范围内使用。

process.env.PORT
process.env.DATABASE_USER
process.env.DATABASE_PASSWORD


PORT=3000 DATABASE_HOST=localhost DATABASE_USER=admin DATABASE_PASSWORD=mypassword node app.js

export PORT=3000
export DATABASE_HOST=localhost
export DATABASE_PORT=27017
export DATABASE_USER=admin
export DATABASE_PASSWORD=mypassword
node app.js

var server = app.listen(process.env.PORT, function() {
});

var mongoClient = new MongoClient(new Server(process.env.DATABASE_HOST, process.env.DATABASE_PORT));

#5


1  

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?

要对配置进行inlcude,它是像需要它的文件中的require('./config.js')一样简单,还是有更好的方法来实现它?

This is the right way to store config files.

这是存储配置文件的正确方法。

The best approach would be to write your entire application like an ordinary node.js module, and write a small start-up file that calls it. This idea also allow you to use different database drivers using dependency injection.

最好的方法是像普通节点一样编写整个应用程序。并编写一个调用它的小型启动文件。这个想法还允许您使用依赖注入来使用不同的数据库驱动程序。

Good, but not perfect solution is the environment. It is shared among all application, so if you have certain data you want to be available to all of them, this is the best bet. But if you have a config for one particular app, not much so.

好的,但不是完美的解决方案是环境。它是在所有应用程序之间共享的,所以如果您有一定的数据,您希望对所有应用程序都可用,这是最好的选择。但是,如果你有一个特定应用的配置,那就没那么重要了。

PS: And please, don't use JSON for this. It's the worst idea possible. :)

PS:请不要使用JSON。这是最坏的想法。:)

#6


1  

I found this a nice way to handle my config, considering different environments:

我发现这是处理配置的好方法,考虑到不同的环境:

config.coffee

config.coffee

exports.setEnvironment = (env) ->
    switch env
        when "development"
            exports.DEBUG_LOG = true
            exports.DB_PORT = '27017'
            # ...
        when "testing"
            exports.DEBUG_ERROR = true
            exports.DEBUG_CLIENT = true
            # ...
        when "production"
            exports.DEBUG_LOG = false
            # ...
        else console.log "environment #{env} not found"

server.coffee:

server.coffee:

config = require('./config')
config.setEnvironment env