Search code examples
node.jsazureazure-web-app-serviceazure-appservice

Unable to run NodeJS app on Azure Windows App Service


I am very new to Azure App Development and trying to deploy an simple Hello World NodeJS app on Azure Windows App Service. This is very basic Hello world app and working fine on local environment without any issue. I am using VS Code and deployed to App service. In App Service I am getting 500.1002 error. What I found after adding console log, code not even going to app.get or app.listen block. I tried to install npm via Kudu also, not worked.

Here is my app.js file -

const express = require('express')
const app = express()
const appport = process.env.APPPORT
//const appport = 3000
console.log('Rit 0')

app.get('/', (req, res) => {
  console.log('Rit 1')
  res.send( 'Hello World!')
  })

app.listen(appport, () => {
  console.log('Rit 2')
  console.log(appport)
  console.log(`Example app listening at http://localhost:${appport}`)
})

This is my web.config -

    <?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^app.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 site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="app.js"/>
        </rule>
      </rules>
    </rewrite>
    
    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled

      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->
  </system.webServer>
</configuration>

This is my package.json - 

    {
  "name": "appaz",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "dependencies": {
    "debug": "~2.6.9",
    "express": "^4.19.2"
  }
}

Here is my iisnode.yml file -

maxNamedPipeConnectionRetry: 24
namedPipeConnectionRetryDelay: 250

Can anyone please help and let me know what exactly I am missing here ??


Solution

  • Use const appport = process.env.PORT || 3000; instead of const appport = process.env.APPPORT.

    I have modified your code as below:

    const express = require('express')
    const app = express()
    const appport = process.env.PORT || 3000;
    console.log('Rit 0')
    
    app.get('/', (req, res) => {
      console.log('Rit 1')
      res.send( 'Hello World!')
      })
    
    app.listen(appport, () => {
      console.log('Rit 2')
      console.log(appport)
      console.log(`Example app listening at Port:${appport}`)
    })
    

    Add the below mentioned application settings under webapp=>settings=>Environment variables=>App Settings:

    WEBSITES_PORT=8080
    SCM_DO_BUILD_DURING_DEPLOYMENT=1
    

    enter image description here

    Deployed the app to Azure:

    enter image description here

    • Able to run the deployed application:

    enter image description here