问题描述
Windows Server 2008 R2 企业版
节点版本 12.13.1
节点窗口版本 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 会在我的数字 codeclient 上失败,总是假设它是数字类型而不是字符串(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();
}