如何通过使用 node

编程入门 行业动态 更新时间:2024-10-26 06:28:38
本文介绍了如何通过使用 node-windows 创建的 Windows 服务传递 dotenv 配置路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

Windows Server 2008 R2 企业版

Windows Server 2008 R2 Enterprise

节点版本 12.13.1

Node version 12.13.1

节点窗口版本 1.0.0-beta.5

node-windows version 1.0.0-beta.5

当我调用我的节点应用程序时,我需要将路径传递给特定的环境文件,而不是在代码中使用 require('dotenv') 来加载环境变量(例如,从默认的 .env 文件).根据启动应用程序的客户(例如,不同的数据库、路径、客户代码等),此环境文件会有所不同.

When I call my node application, instead of using require('dotenv') in code to load the environment variables (e.g. from a default .env file), I need to pass the path to a specific environment file. This environemnt file is different depending for which customer the application is started for (e.g. different database, paths, customer code, etc..).

在 cli 中,我可以这样成功地做到这一点:

In the cli, I can succesfully do it this way:

node --require dotenv/config bin/www dotenv_config_path=C:\projects\abc\env\XYZ.env

如您所见,我使用绝对路径作为 env 文件的位置,但它也适用于相对路径.我只是想消除这个原因,因为我无法使用 node-windows 进行这项工作.

As you can see I use an absolute path for the location of the env file, but it also works with a relative path. I'm just trying to eliminate this as a reason why I can't make this work with node-windows.

我正在尝试使用 node-windows 创建一个 Windows 服务包装器,该包装器调用我的节点应用程序,并像上面的代码一样加载特定的 env 文件.到目前为止无法使其工作,在创建 Windows 服务后,它会在片刻后退出,这告诉我它缺少运行所需的环境变量.这意味着它无法加载或找到环境文件.

I'm trying to use node-windows to create a Windows service wrapper that calls my node application and also loads a specific env file like the above code does. Can't make it work so far, after creating the Windows service, it quits after a moment, which tells me it's missing the environment variables it needs to function. Which means it can't load of find the environment file.

这是我使用 node-windows 创建 Windows 服务的脚本:

Here is my script to create the Windows service using node-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"和scriptOptions".方法和环境"各种配置中的方法,但没有任何效果.

I've tried both the "scriptOptions" approach and the "env" approach in various configurations, but nothing works.

如果之前有人成功完成过这样的工作,我非常想知道您是如何做到的.

If anyone has managed to make something like this work before, I'd very much like to know how you did it.

推荐答案

所以我最终这样做的方法是通过 node-windows 的 scriptOptions 传递我的 codeclient 变量,然后在我的节点应用程序中使用它dotenv 加载特定的 env 文件.其实更简单.

So the way I ended up doing this is instead just pass my codeclient variable through the scriptOptions of node-windows, and then using that in my node application to have dotenv load a specific env file. It's more simple really.

我对这种方法的唯一问题是 node-windows 会在我的数字 codeclient 上失败,总是假设它是数字类型而不是字符串(node-windows 稍后会尝试调用 String.split() ).我不得不在前面附加下划线以将其强制为字符串.

The only issue I had with the approach is that node-windows would fail with my numerical codeclient, always assuming it's a number type instead of a string (node-windows tries to call String.split() on it later). I had to append the underscore in front to force it as a string.

使用 node-windows 创建 Windows 服务的脚本:

Script to create the Windows service with node-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 文件:

Loading the env file in the node application:

// 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();
}

这篇关于如何通过使用 node-windows 创建的 Windows 服务传递 dotenv 配置路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 19:54:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1123431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!