简体   繁体   English

无法从Node.js读取Azure配置设置

[英]Can't read Azure configuration settings from Node.js

I'm running a Node.js application in Azure and am trying to get configuration settings like this: 我在Azure中运行Node.js应用程序,并尝试获取如下配置设置:

var azure = require('azure');

azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
   if (error) {
      console.log(error);
      return;
   }

   console.log('Settings: %s', settings);
});

But the output is always this: 但输出始终如下:

{ "code": "ENOENT", "errno": "ENOENT", "syscall": "connect", "fileName": "\\\\.\\pipe\\WindowsAzureRuntime" }

I'm running Node.Js within IIS using IISNode using all the latest bits. 我使用IISNode使用所有最新位在IIS中运行Node.Js. I also get the same error if I run node manually on the Azure VM (ie node.exe server.js). 如果我在Azure VM上手动运行节点(即node.exe server.js),我也会收到同样的错误。 Same when running on my PC in the Azure Development Fabric. 在Azure Development Fabric中运行我的PC时也一样。

Thanks in advance for any help or suggestions! 在此先感谢您的任何帮助或建议!

Since you mentioned you're running in IISNode, it must be a web role. 由于您提到您在IISNode中运行,因此它必须是Web角色。 Note the following from the SDK readme : 请注意SDK自述文件中的以下内容:

The Service Runtime allows you to interact with the machine environment where the current role is running. Service Runtime允许您与运行当前角色的计算机环境进行交互。 Please note that these commands will only work if your code is running in a worker role inside the Azure emulator or in the cloud. 请注意, 只有当您的代码在Azure模拟器内部或云中以辅助角色运行时 ,这些命令才有效

Here is my solution. 这是我的解决方案。 It is not good, but at least it works for now. 这不好,但至少它现在有效。

  1. Create a .NET console application like this 像这样创建一个.NET控制台应用程序

    using Microsoft.WindowsAzure; 使用Microsoft.WindowsAzure; using System; 使用系统; using System.Collections.Generic; 使用System.Collections.Generic; using System.Linq; 使用System.Linq; using System.Text; 使用System.Text; using System.Threading; 使用System.Threading; using System.Threading.Tasks; 使用System.Threading.Tasks;

    namespace CloudConfigurationHelper { class Program { static int MaxRetryTime = 10; namespace CloudConfigurationHelper {class Program {static int MaxRetryTime = 10;

      public static Dictionary<string, string> GetSettings(string[] keys) { Dictionary<string, string> settings = new Dictionary<string, string>(); try { foreach (string key in keys) { settings[key] = CloudConfigurationManager.GetSetting(key); } } catch { MaxRetryTime--; if (MaxRetryTime <= 0) { return settings; } Thread.Sleep(2000); return GetSettings(keys); } return settings; } static int Main(string[] args) { var settings = GetSettings(args); if (settings.Count != args.Length) { return -1; } foreach (string key in settings.Keys) { Console.WriteLine(key + "=" + settings[key]); } return 0; } } 

    } }

  2. Put start-up task to read the azure configuration variable and write to .env file. 启动任务以读取azure配置变量并写入.env文件。 Use https://github.com/motdotla/dotenv to read .env file and load to process.env. 使用https://github.com/motdotla/dotenv读取.env文件并加载到process.env。

Env.cmd file: Env.cmd文件:

@echo off
cd %~dp0
CloudConfigurationHelper.exe %*

Add start-up task to ServiceDefinition.csdef: 将启动任务添加到ServiceDefinition.csdef:

<Task commandLine="Env.cmd YOUR_SETTING_KEY &gt; ..\.env executionContext="elevated" />
  1. In NodeJS web role, we just need to read this variable via process.env["YOUR_SETTING_KEY"] 在NodeJS Web角色中,我们只需要通过process.env["YOUR_SETTING_KEY"]读取此变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM