简体   繁体   English

是否有可能确保iisnode尊重Azure中的NODE_PATH?

[英]Is it possible to ensure iisnode respects NODE_PATH in Azure?

It is not uncommon to want to be able to have non-relative imports, for example, for configuration, etc... 希望能够进行非相对导入(例如配置等)的情况并不少见......

In the world of running a node executable on your own (development env, any cloud provider... things like that) you can simply set an env var and have it respected by the node runtime. 在您自己运行节点可执行文件的世界中(开发环境,任何云提供程序......类似的东西),您只需设置一个env var并让节点运行时尊重它。

Imagine a project structure like so: dist |--foo |--bar |--baz app.js |--config 想象一下这样的项目结构:dist | --foo | --bar | --baz app.js | --config

in app.js with NODE_PATH=dist, I can simply require('config') and have what I need. 在app.js中使用NODE_PATH = dist,我可以简单地要求('config')并拥有我需要的东西。

Within Azure App Services, it appears to ignore NODE_PATH from Application Settings. 在Azure App Services中,它似乎忽略了“应用程序设置”中的NODE_PATH。 Is something missing or is this not possible? 是缺少什么或这是不可能的?

In Azure App Services, you can set the NODE_PATH environment variable in the Azure portal with the following steps. 在Azure App Services中,您可以使用以下步骤在Azure门户中设置NODE_PATH环境变量。

1, Create the D:\\home\\site\\my_node_modules\\config directory and put the index.js file in where. 1,创建D:\\home\\site\\my_node_modules\\config目录并将index.js文件放在where。 In this case, I just export the "name" variable. 在这种情况下,我只导出“name”变量。

// D:\home\site\my_node_modules\config\index.js
var name = "foobar";
// export it
exports.name = name;

2, Navigate to your App Service in the Azure portal , click on Application settings in the SETTING menu and then set the NODE_PATH variable as below: 2,在Azure门户中导航到App Service,单击SETTING菜单中的Application settings ,然后设置NODE_PATH变量,如下所示:

在此输入图像描述

3, In the app.js file, you can simply require('config') like this: 3,在app.js文件中,你可以简单地require('config')这样:

var http = require('http')
var config = require('config')

http.createServer(function (req, res) {
    res.end(config.name)
}).listen(process.env.PORT || 3000)

4, At last, it works fine. 4,最后,它工作正常。

在此输入图像描述

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

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