如何通过使用 node-windows 创建的 Windows 服务传递 dotenv 配置路径
How to pass the dotenv config path through a Windows service created with node-windows
Windows 服务器 2008 R2 企业版
节点版本 12.13.1
node-windows 版本 1.0.0-beta.5
当我调用我的节点应用程序时,我需要将路径传递到特定环境,而不是在代码中使用 require('dotenv') 来加载环境变量(例如从默认的 .env 文件)文件。此环境文件根据为哪个客户启动应用程序而有所不同(例如,不同的数据库、路径、客户代码等)。
在cli中,我这样可以成功:
node --require dotenv/config bin/www dotenv_config_path=C:\projects\abc\env\XYZ.env
如您所见,我使用绝对路径作为 env 文件的位置,但它也适用于相对路径。我只是想消除这个原因,因为我无法使用 node-windows.
我正在尝试使用 node-windows 创建一个 Windows 服务包装器,它调用我的节点应用程序并像上面的代码一样加载特定的 env 文件。到目前为止无法让它工作,在创建 Windows 服务后,它会在片刻后退出,这告诉我它缺少运行所需的环境变量。这意味着它无法加载或找到环境文件。
这是我使用 node-windows:
创建 Windows 服务的脚本
#!/usr/bin/env node
// Usage:
// npm run install-win XYZ
// Notes:
// 1. Before creating the windows service, make sure to delete any previous files in the /bin folder (i.e. all files abcXYZ.*)
// 2. After creating the windows service, change the Log On account to the ******* user to avoid persmission issues when using paths on other production servers
const args = process.argv;
const codeclient = args[2];
const serviceName = `abc${codeclient}`;
const environmentPath = `C:\projects\abc\abc-api\env\${codeclient}.env`; // Make sure to use the full absolute path here
const Service = require('node-windows').Service;
// Create a new service object
const svc = new Service({
name: serviceName,
description: serviceName,
script: require('path').join(__dirname, 'www'),
scriptOptions: `dotenv_config_path=${environmentPath}`,
nodeOptions: [
'--require=dotenv/config',
'--harmony',
'--max_old_space_size=4096'
]/*,
env: {
name: 'DOTENV_CONFIG_PATH',
value: environmentPath
}*/
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function(){
svc.start();
});
svc.install();
我在各种配置中尝试了“scriptOptions”方法和“env”方法,但没有任何效果。
如果以前有人成功地完成了类似的工作,我非常想知道你是怎么做到的。
所以我最终这样做的方法是通过 node-windows 的 scriptOptions 传递我的 codeclient 变量,然后在我的节点应用程序中使用它来让 dotenv 加载特定的 env 文件。真的更简单。
我对该方法的唯一问题是 node-windows 会在我的数字代码客户端上失败,始终假设它是数字类型而不是字符串(node-windows 试图调用String.split() 稍后再说)。我不得不在前面附加下划线以将其强制为字符串。
使用 node-windows 创建 Windows 服务的脚本:
#!/usr/bin/env node
// Usage:
// npm run install-win 123
// Notes:
// 1. Before creating the windows service, make sure to delete any previous files in the /bin folder (i.e. all files abc123.*)
// 2. After creating the windows service, change the Log On account of the service to the ******** user to avoid persmission issues when using paths on other production servers
const args = process.argv;
const codeclient = args[2];
const serviceName = `abc${codeclient}`;
const Service = require('node-windows').Service;
// Create a new service object
const svc = new Service({
name: serviceName,
description: serviceName,
script: require('path').join(__dirname, 'www'),
scriptOptions: `_${codeclient}`,
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function(){
svc.start();
});
svc.install();
正在节点应用程序中加载 env 文件:
// Load environment variables into process.env
if (process.argv[2]) {
// Load a specific env file for the codeclient passed as argument
const codeclient = process.argv[2].replace('_', '');
require('dotenv').config({ path: `./env/${codeclient}.env` });
}
else {
// Load the default .env file
require('dotenv').config();
}
Windows 服务器 2008 R2 企业版
节点版本 12.13.1
node-windows 版本 1.0.0-beta.5
当我调用我的节点应用程序时,我需要将路径传递到特定环境,而不是在代码中使用 require('dotenv') 来加载环境变量(例如从默认的 .env 文件)文件。此环境文件根据为哪个客户启动应用程序而有所不同(例如,不同的数据库、路径、客户代码等)。
在cli中,我这样可以成功:
node --require dotenv/config bin/www dotenv_config_path=C:\projects\abc\env\XYZ.env
如您所见,我使用绝对路径作为 env 文件的位置,但它也适用于相对路径。我只是想消除这个原因,因为我无法使用 node-windows.
我正在尝试使用 node-windows 创建一个 Windows 服务包装器,它调用我的节点应用程序并像上面的代码一样加载特定的 env 文件。到目前为止无法让它工作,在创建 Windows 服务后,它会在片刻后退出,这告诉我它缺少运行所需的环境变量。这意味着它无法加载或找到环境文件。
这是我使用 node-windows:
创建 Windows 服务的脚本#!/usr/bin/env node
// Usage:
// npm run install-win XYZ
// Notes:
// 1. Before creating the windows service, make sure to delete any previous files in the /bin folder (i.e. all files abcXYZ.*)
// 2. After creating the windows service, change the Log On account to the ******* user to avoid persmission issues when using paths on other production servers
const args = process.argv;
const codeclient = args[2];
const serviceName = `abc${codeclient}`;
const environmentPath = `C:\projects\abc\abc-api\env\${codeclient}.env`; // Make sure to use the full absolute path here
const Service = require('node-windows').Service;
// Create a new service object
const svc = new Service({
name: serviceName,
description: serviceName,
script: require('path').join(__dirname, 'www'),
scriptOptions: `dotenv_config_path=${environmentPath}`,
nodeOptions: [
'--require=dotenv/config',
'--harmony',
'--max_old_space_size=4096'
]/*,
env: {
name: 'DOTENV_CONFIG_PATH',
value: environmentPath
}*/
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function(){
svc.start();
});
svc.install();
我在各种配置中尝试了“scriptOptions”方法和“env”方法,但没有任何效果。
如果以前有人成功地完成了类似的工作,我非常想知道你是怎么做到的。
所以我最终这样做的方法是通过 node-windows 的 scriptOptions 传递我的 codeclient 变量,然后在我的节点应用程序中使用它来让 dotenv 加载特定的 env 文件。真的更简单。
我对该方法的唯一问题是 node-windows 会在我的数字代码客户端上失败,始终假设它是数字类型而不是字符串(node-windows 试图调用String.split() 稍后再说)。我不得不在前面附加下划线以将其强制为字符串。
使用 node-windows 创建 Windows 服务的脚本:
#!/usr/bin/env node
// Usage:
// npm run install-win 123
// Notes:
// 1. Before creating the windows service, make sure to delete any previous files in the /bin folder (i.e. all files abc123.*)
// 2. After creating the windows service, change the Log On account of the service to the ******** user to avoid persmission issues when using paths on other production servers
const args = process.argv;
const codeclient = args[2];
const serviceName = `abc${codeclient}`;
const Service = require('node-windows').Service;
// Create a new service object
const svc = new Service({
name: serviceName,
description: serviceName,
script: require('path').join(__dirname, 'www'),
scriptOptions: `_${codeclient}`,
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function(){
svc.start();
});
svc.install();
正在节点应用程序中加载 env 文件:
// Load environment variables into process.env
if (process.argv[2]) {
// Load a specific env file for the codeclient passed as argument
const codeclient = process.argv[2].replace('_', '');
require('dotenv').config({ path: `./env/${codeclient}.env` });
}
else {
// Load the default .env file
require('dotenv').config();
}