【问题标题】:Port being undefined in the Azure Node.js application running on IISNode在 IISNode 上运行的 Azure Node.js 应用程序中未定义端口
【发布时间】:2020-10-20 00:11:55
【问题描述】:

我有一个使用 IISNode 运行 Node.js 应用程序的 Azure 应用服务。问题是 process.env.PORT 未定义。我读过 IISNode 使用了一个名为 命名管道 的东西,并且端口信息可能不容易读取(?),但在我的情况下,我只是未定义。

我尝试部署的项目可以从GitHub找到。

我确实定义了一个 Web.config 文件,它看起来像这样:

    <handlers>
        <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
        <add name="iisnode" path="index.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
        <rules>
            <!-- Don't interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="^index.js\/debug[\/]?" />
            </rule>

            <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
            <rule name="StaticContent">
                <action type="Rewrite" url="public{REQUEST_URI}" />
            </rule>

            <!-- All other URLs are mapped to the Node.js application entry point -->
            <rule name="DynamicContent">
                <match url="/*" />
                <action type="Rewrite" url="index.js" />
            </rule>
        </rules>
    </rewrite>

    <!-- You can control how Node is hosted within IIS using the following options -->
                <!--<iisnode      
                  node_env="%node_env%"
                  nodeProcessCountPerApplication="1"
                  maxConcurrentRequestsPerProcess="1024"
                  maxNamedPipeConnectionRetry="3"
                  namedPipeConnectionRetryDelay="2000"      
                  maxNamedPipeConnectionPoolSize="512"
                  maxNamedPipePooledConnectionAge="30000"
                  asyncCompletionThreadCount="0"
                  initialRequestBufferSize="4096"
                  maxRequestBufferSize="65536"
                  watchedFiles="*.js"
                  uncFileChangesPollingInterval="5000"      
                  gracefulShutdownTimeout="60000"
                  loggingEnabled="true"
                  logDirectoryNameSuffix="logs"
                  debuggingEnabled="true"
                  debuggerPortRange="5058-6058"
                  debuggerPathSegment="debug"
                  maxLogFileSizeInKB="128"
                  appendToExistingLog="false"
                  logFileFlushInterval="5000"
                  devErrorsEnabled="true"
                  flushResponse="false"      
                  enableXFF="false"
                  promoteServerVars=""
                 />-->
    <iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade;views\account\*.jade;iisnode.yml" />
</system.webServer>

我编写了一个 Kudu 脚本,它将构建资产并将它们复制到 %DEPLOYMENT_TARGET%。

我在这里缺少什么?任何帮助表示赞赏!

更新

我花了好几天的时间来找出进程无法启动的原因,因此未定义环境变量。我在 15 分钟内就在 Heroku 上启动并运行了应用程序,所以我想这仍然是个谜(至少对我来说)。

【问题讨论】:

  • 尝试使用 Kudu 进程浏览器查看 Node 进程的 env 变量。您应该看到 PORT 的值类似于 \\.\pipe\e317fe18-7017-4a09-84fb-773a54cf0738。另外,请尝试使用普通的 Node 应用程序进行隔离。
  • @DavidEbbo 有没有办法查看 IIS 日志?我只能在列表中看到 SCM 进程,但我不知道 IISNode / IIS 是否正在尝试启动我的应用程序,即使我定义了 web.config
  • 试试this,看看有没有有趣的日志。如上所述,请用一个简单的案例进行隔离。

标签: node.js azure iis azure-web-app-service iisnode


【解决方案1】:

“Starter Site”应用程序没有为我部署,看起来它的许多依赖项现在都已损坏。话虽如此,以下是您开始使用 Azure 应用服务所需的最少内容:

var http = require('http');

function onRequest(request, response) {
    response.writeHead(200, {
        'Content-type': 'text-plain'
    });
    response.write('Hello from Node.');
    response.end();
}

// provess.env.PORT will expand to the name pipe value on App Service
// request --> Frontends (ARR) --> Web Worker (IIS) --> iisnode --> 
//    --named-pipe--> node.exe server.js

http.createServer(onRequest).listen(process.env.PORT || 3000);
console.log('Listening for requests on port ' + (process.env.PORT || 3000));

您不必担心process.env.PORT 返回的内容,它由平台在运行时处理,并保证返回正确的内容。

下面是事情的真相:

Kudu(.scm 站点)→ Process Explorer → node.exe → 属性 → 环境变量:

【讨论】:

  • 感谢您的回答!我链接的存储库确实是 Starter Site 的一个分支,但在分支之后进行了修改。我认为答案并不能解决我的整个 process.env 未定义的问题。
  • 如果您的应用程序未正确启动,则不会有任何process.env.PORT。它是在运行时动态创建的。请参阅我刚刚添加到我的答案中的屏幕截图。
  • 有没有办法使用 express 模块来实现这一点?
  • 为什么有什么不同?
猜你喜欢
  • 1970-01-01
  • 2017-06-29
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2018-10-13
  • 2018-01-12
相关资源
最近更新 更多