在运行在IISNode上的Azure Node.js应用程序中,端口未定义。

3
我有一个使用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%。

我错过了什么?任何帮助都将不胜感激!

更新

我花费了数小时的时间,在多个日期内找出了导致进程未启动,因此环境变量未定义的原因。我在Heroku上运行应用程序只花了15分钟,所以我猜这仍然是一个谜(至少对我来说是这样)。


尝试使用Kudu进程资源管理器查看Node进程的环境变量。您应该会看到一个名为PORT的值,它看起来像 \\.\pipe\e317fe18-7017-4a09-84fb-773a54cf0738。此外,尝试使用一个微不足道的Node应用进行隔离。 - David Ebbo
@DavidEbbo 有没有办法查看IIS日志?我只能在列表中看到SCM进程,但是我不知道是否已经定义了web.config,IISNode / IIS是否尝试启动我的应用程序。 - Tx3
尝试使用此链接并查看是否有任何有趣的日志。如上所建议,请使用简单的案例进行隔离。 - David Ebbo
1个回答

4

“Starter Site”应用程序无法部署,似乎它的许多依赖项现在都已经失效。话虽如此,以下是你需要在Azure App Service上开始运行的最小内容:

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 site) → Process Explorer → node.exe → Properties → Environment variables。请参考下图: App Service with NodeJSProcess Explorer

谢谢您的回答!我链接的仓库确实是从 Starter Site Fork 的,但自从 Fork 以来已经进行了修改。我认为这个回答并没有解决我整个 process.env 都未定义的问题。 - Tx3
如果您的应用程序启动不正确,就不会有process.env.PORT。它是在运行时动态创建的。请参见我刚刚添加到答案中的截图。 - evilSnobu
是否有一种方法可以使用Express模块使其正常工作? - Isaac Vidrine
为什么这有什么不同呢? - evilSnobu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接