3

I have an Azure App Service running Node.js application using IISNode. The problem is that process.env.PORT is undefined. I have read that IISNode uses a thing called named pipes and that the port information might not be easily readable (?), but in my case I only get undefined.

The project I try to deploy can be found from GitHub.

I do have a Web.config file defined and it looks like this:

    <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>

I have written a Kudu scripts that will build the assets and copy them to %DEPLOYMENT_TARGET%.

What is it that I am missing here? Any help is appreciated!

Update

I spent hours on multiple days to find out what could be the cause why the process doesn't start and therefore env variables are undefined. I got the app up and running on Heroku in 15 minutes, so I guess this stays mystery (for me at least).

Tx3
  • 6,796
  • 4
  • 37
  • 52
  • Try using Kudu process explorer to look at the env variables of the Node process. You should see PORT with a value that looks like `\\.\pipe\e317fe18-7017-4a09-84fb-773a54cf0738`. Also, try isolating by using a trivial Node app. – David Ebbo Apr 17 '17 at 15:27
  • @DavidEbbo is there any way to see IIS logs? I can see only SCM process on the list, but I have no idea is the IISNode / IIS trying to start my app even if I have web.config defined – Tx3 Apr 22 '17 at 19:13
  • Try [this](https://github.com/projectkudu/kudu/wiki/Troubleshooting-Node-errors) and see if you get any interesting log. And as suggested above, please isolate with a trivial case. – David Ebbo Apr 22 '17 at 19:15

1 Answers1

4

The "Starter Site" app does not deploy for me, looks like many of its dependencies are now broken. That being said, here's the minimal stuff you need to get going on 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));

You don't have to worry about what process.env.PORT returns, it's handled by the platform at runtime and it's guaranteed to return the right thing.

Here's how things look under the hood:

App Service with NodeJS

Kudu (.scm site) → Process Explorer → node.exe → Properties → Environment variables:

Process Explorer

evilSnobu
  • 24,582
  • 8
  • 41
  • 71
  • Thank you for answering! The repository that I linked is indeed a fork from Starter Site, but modified since forking. I think the answer doesn't solve my problem of having whole process.env being undefined. – Tx3 Apr 17 '17 at 13:49
  • If your application does not start up correctly, there won't be any `process.env.PORT`. It's created dynamically at runtime. See the screenshot i've just added to my answer. – evilSnobu Apr 17 '17 at 16:34
  • Is there a way for this to work using express module? – Isaac Vidrine Oct 22 '18 at 02:58
  • Why is that any different? – evilSnobu Oct 22 '18 at 05:36