node.js 配置文件的 Module.exports 与普通 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18476765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Module.exports vs plain json for config files
提问by Nam Nguyen
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 有什么好处吗?有什么区别?
回答by Peter Lyons
javascript CommonJS Module
javascript CommonJS 模块
- comments
- conditionals
- loops and such to populate defaults
- code to change config based on NODE_ENV or similar
- code to look for external files for SSL keys, API credentials, etc
- easier to have fallbacks and defaults
- 注释
- 条件句
- 循环等填充默认值
- 基于 NODE_ENV 或类似内容更改配置的代码
- 用于查找 SSL 密钥、API 凭据等的外部文件的代码
- 更容易有回退和默认值
JSON file
JSON 文件
- 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 文件。
回答by Nam Nguyen
Thanks @jonathan-ong, looks like config.js (NOT JSON file) works as expected and I could put some comments.
谢谢 @jonathan-ong,看起来 config.js(不是 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'
}
}
};
回答by TGW
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 JSONfor config files.
正如@Peter Lyons 提到的,.js 文件有自己的特权。但是,如果我不必访问 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
并使用 config 模块来消费这些配置

