I see multiple ways to create config files in Node.js. One uses module.exports in js file, one just use plain json object.
我看到在Node.js中创建配置文件的多种方法。一个在js文件中使用module.exports,一个只使用普通的json对象。
// config1.js
module.exports = {
config_1: "value 1",
config_2: "value 2"
}
// config2.json
{
"config_1": "value 1",
"config_2": "value 2"
}
Is there any advantages of using module.exports in config file? What are the differences?
在配置文件中使用module.exports有什么好处吗?有什么区别?
3 个解决方案
#1
22
javascript CommonJS Module
- comments
- 注释
- conditionals
- 条件语句
- loops and such to populate defaults
- 循环等,以填充默认值
- code to change config based on NODE_ENV or similar
- 用于根据NODE_ENV或类似内容更改配置的代码
- code to look for external files for SSL keys, API credentials, etc
- 用于查找SSL密钥,API凭据等外部文件的代码
- easier to have fallbacks and defaults
- 更容易有后备和默认值
JSON file
- easy to parse and update with external tools
- 易于使用外部工具进行解析和更新
- compatible with pretty much every programming language out there
- 几乎兼容所有编程语言
- pure data that can be loaded without being executed
- 可以在不执行的情况下加载的纯数据
- easy to pretty print
- 易于打印
- JSON could start as the basis and all the code items described above about CommonJS module could live in a config.js module that reads config.json as it's starting point
- JSON可以作为基础开始,上面描述的关于CommonJS模块的所有代码项都可以存在于config.js模块中,该模块读取config.json作为它的起点
So I always start with a commonjs module for the convenience, but keep any logic in there simple. If your config.js has bugs and needs tests, it's probably too complicated. KISS. If I know for a fact other things are going to want poke around in my config, I'll use a JSON file.
所以我总是从一个commonjs模块开始,以方便,但保持任何逻辑简单。如果你的config.js有bug并需要测试,那可能太复杂了。吻。如果我知道其他事情需要在我的配置中查找,我将使用JSON文件。
#2
8
Thanks @jonathan-ong, looks like config.js (NOT JSON file) works as expected and I could put some comments.
谢谢@ jonathan-ong,看起来像config.js(NOT JSON文件)按预期工作,我可以发表一些评论。
module.exports = {
// Development Environment
development: {
database: {
host: '127.0.0.1',
login: 'dev',
password: 'dev'
}
},
// Production Environment
production: {
database: {
host: '127.0.0.1',
login: 'prod',
password: 'prod'
}
}
};
#3
0
js files have their own perks as @Peter Lyons mentioned. But if I don't have to access external resources for API keys and etc. I would prefer JSON for config files.
js文件有自己的特权,如提到的@Peter Lyons。但是,如果我不必访问API密钥等的外部资源,我更喜欢配置文件的JSON。
Simple reason being I would not have to touch my code for just for the sake of making changes to config files. I can simply make an API to edit these json files to add,update or remove any configuration key-value pair. Even add whole new config file for a separate environment using an API.
简单的原因是我不必为了更改配置文件而触摸我的代码。我可以简单地创建一个API来编辑这些json文件,以添加,更新或删除任何配置键值对。甚至可以使用API为单独的环境添加全新的配置文件。
And use config module to consume these configurations
并使用配置模块来使用这些配置
#1
22
javascript CommonJS Module
- comments
- 注释
- conditionals
- 条件语句
- loops and such to populate defaults
- 循环等,以填充默认值
- code to change config based on NODE_ENV or similar
- 用于根据NODE_ENV或类似内容更改配置的代码
- code to look for external files for SSL keys, API credentials, etc
- 用于查找SSL密钥,API凭据等外部文件的代码
- easier to have fallbacks and defaults
- 更容易有后备和默认值
JSON file
- easy to parse and update with external tools
- 易于使用外部工具进行解析和更新
- compatible with pretty much every programming language out there
- 几乎兼容所有编程语言
- pure data that can be loaded without being executed
- 可以在不执行的情况下加载的纯数据
- easy to pretty print
- 易于打印
- JSON could start as the basis and all the code items described above about CommonJS module could live in a config.js module that reads config.json as it's starting point
- JSON可以作为基础开始,上面描述的关于CommonJS模块的所有代码项都可以存在于config.js模块中,该模块读取config.json作为它的起点
So I always start with a commonjs module for the convenience, but keep any logic in there simple. If your config.js has bugs and needs tests, it's probably too complicated. KISS. If I know for a fact other things are going to want poke around in my config, I'll use a JSON file.
所以我总是从一个commonjs模块开始,以方便,但保持任何逻辑简单。如果你的config.js有bug并需要测试,那可能太复杂了。吻。如果我知道其他事情需要在我的配置中查找,我将使用JSON文件。
#2
8
Thanks @jonathan-ong, looks like config.js (NOT JSON file) works as expected and I could put some comments.
谢谢@ jonathan-ong,看起来像config.js(NOT JSON文件)按预期工作,我可以发表一些评论。
module.exports = {
// Development Environment
development: {
database: {
host: '127.0.0.1',
login: 'dev',
password: 'dev'
}
},
// Production Environment
production: {
database: {
host: '127.0.0.1',
login: 'prod',
password: 'prod'
}
}
};
#3
0
js files have their own perks as @Peter Lyons mentioned. But if I don't have to access external resources for API keys and etc. I would prefer JSON for config files.
js文件有自己的特权,如提到的@Peter Lyons。但是,如果我不必访问API密钥等的外部资源,我更喜欢配置文件的JSON。
Simple reason being I would not have to touch my code for just for the sake of making changes to config files. I can simply make an API to edit these json files to add,update or remove any configuration key-value pair. Even add whole new config file for a separate environment using an API.
简单的原因是我不必为了更改配置文件而触摸我的代码。我可以简单地创建一个API来编辑这些json文件,以添加,更新或删除任何配置键值对。甚至可以使用API为单独的环境添加全新的配置文件。
And use config module to consume these configurations
并使用配置模块来使用这些配置