I have deployed a node app in Azure and want to configure it. This is the content of the file "index.js" that I want to test:
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const cors = require('cors');
var cats = [{name:'Lily'}, {name: 'Lucy'}];
var corOptions = {
origin: '*',
optionsSuccessStatus: 200
};
app.use(cors(corOptions));
app.listen(3000,() =>{
console.log('Server started :)');
});
app.route('/api/cats').get((req, res) =>{
console.log(req.hostname);
console.log(req);
res.send(cats);
});
console.log("Server is running...");
Here's package.json file
{
"name": "app-service-hello-world",
"description": "Simple Hello World Node.js sample for Azure App Service",
"version": "0.0.1",
"private": true,
"license": "MIT",
"author": "Microsoft",
"engines": {
"node": ">=6.9.1"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"cors": "^2.8.4",
"express": "^4.16.3"
}
}
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<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="index.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="^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 site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.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" />
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
</configuration>
I'm testing the application using following below URL but not getting the expected output: https://firstnodeapp1.azurewebsites.net/api/cats
I tested it locally as http://localhost:8000/api/cats and it works fine. So, actually concerned about what's missing.
I would really appreciate any help to configure it properly.
Change port to process.env.port
and redeploy your code should work.
Explanation:
Azure Web App is a model of sand box, which is accessed via the only two already-exposed 80(HTTP) and 443(HTTPS) TCP ports.
Using process.env.port
, Azure will create a named pipe for your Nodejs server to listen, and pass the request from 443 port to the named pipe. Any specific port inside your app is invalid and will cause Server Internal Error(500).