使必需的 json 在其他模块中可用
Make required json available in other modules
我正在编写一个在 json 文件中具有配置参数的应用程序。像这样:
// config.json
{
"httpServer": {
"port": 3000
},
"module1": {
"setting1": "value1",
"setting2": "value2"
},
"module2": {
"setting1": "value1",
"setting2": "value2"
}
}
// index.js
const config = require("./config")
const func1 = require("./module1")
const func2 = require("./module2")
// code here
// module1.js
const config = require("./config")
// use config and define functions
module.exports = {
function: function
}
// module2.js
const config = require("./config")
// use config and define functions
module.exports = {
function: function
}
问题是我在每个模块中都需要这个文件,这使得我的代码无法维护,因为如果文件名更改,我需要更新每个 require 语句。我很确定这不是 "correct" 的做法。是否可以在程序启动时require一次配置文件,然后在其他模块中引用它?或者我应该将配置文件作为命令行参数传递,然后在需要该文件时使用 process.argv 数组吗?处理此类情况的最佳方法是什么?
使用 dotenv 包 npm install dotenv --save
,
创建配置文件
//config.env
NODE_ENV=development
IP=127.0.0.1
PORT=3000
加载配置文件
//index.js
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' })
随时随地使用它
//module1
console.log('IP: ',process.env.IP)
老实说,我真的没有发现在多个文件中要求配置有什么问题。既然你需要它,你就需要它。
如果实在不想require多次,可以考虑这个
将函数样式转换为 class 样式,然后将配置作为 class
的依赖项注入
主文件
const config = require("./config");
const file1 = new File1(config);
const file2 = new File2(config);
文件 1
class File1 {
constructor(config) {
this.config_ = config;
}
someFunction() {
// use this.config_ here
}
}
文件 2
class File2 {
constructor(config) {
this.config_ = config;
}
someFunction() {
// use this.config_ here
}
}
使用这种方法的几个优点是:
- 更好的可测试性,因为您可以根据需要模拟配置。
- 你也可以在一个地方换,你在注射时不需要换其他地方。
我正在编写一个在 json 文件中具有配置参数的应用程序。像这样:
// config.json
{
"httpServer": {
"port": 3000
},
"module1": {
"setting1": "value1",
"setting2": "value2"
},
"module2": {
"setting1": "value1",
"setting2": "value2"
}
}
// index.js
const config = require("./config")
const func1 = require("./module1")
const func2 = require("./module2")
// code here
// module1.js
const config = require("./config")
// use config and define functions
module.exports = {
function: function
}
// module2.js
const config = require("./config")
// use config and define functions
module.exports = {
function: function
}
问题是我在每个模块中都需要这个文件,这使得我的代码无法维护,因为如果文件名更改,我需要更新每个 require 语句。我很确定这不是 "correct" 的做法。是否可以在程序启动时require一次配置文件,然后在其他模块中引用它?或者我应该将配置文件作为命令行参数传递,然后在需要该文件时使用 process.argv 数组吗?处理此类情况的最佳方法是什么?
使用 dotenv 包 npm install dotenv --save
,
创建配置文件
//config.env
NODE_ENV=development
IP=127.0.0.1
PORT=3000
加载配置文件
//index.js
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' })
随时随地使用它
//module1
console.log('IP: ',process.env.IP)
老实说,我真的没有发现在多个文件中要求配置有什么问题。既然你需要它,你就需要它。
如果实在不想require多次,可以考虑这个
将函数样式转换为 class 样式,然后将配置作为 class
的依赖项注入主文件
const config = require("./config");
const file1 = new File1(config);
const file2 = new File2(config);
文件 1
class File1 {
constructor(config) {
this.config_ = config;
}
someFunction() {
// use this.config_ here
}
}
文件 2
class File2 {
constructor(config) {
this.config_ = config;
}
someFunction() {
// use this.config_ here
}
}
使用这种方法的几个优点是:
- 更好的可测试性,因为您可以根据需要模拟配置。
- 你也可以在一个地方换,你在注射时不需要换其他地方。