Running a NodeJS and Express 4 App in Azure
I encountered 500 errors when I tried to move my Node/Express app to Azure. It ran fine on Heroku, but threw 500's when I ran it on Azure WebApps. I was using git deployment.
In order to get a NodeJS and Express 4 app running in Azure, follow these steps:
Profile
Create a file called Procfile
in your root folder
Configure the file as follows (eg if app.js is in your root folder):
web: node app.js
If app.js exists in a subfolder such as /Server/app.js
, then you would configure the file as:
web: node server/app.js
Web.config
Create a web.config file and place it in the root. Adjust the server/app.js
or app.js
as needed (like mentioned in the step above). This will also redirect from HTTP to HTTPS (which you might/might not want)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="server/app.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server/app.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server/app.js"/>
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>