Øyvind Bråthen Øyvind Bråthen - 3 months ago 10
Node.js Question

Getting a node.js application hosted on GitHub to run in Azure properly

I'm trying to host a node.js application hosted on GitHub in Azure. I have made it down to the minimal example that still fails.

This is my

./server/index.js
file:

var http = require('http');

var server = http.createServer(function (request, response){
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello world!\n");
});

server.listen(80);
console.log("Server running at http://127.0.0.1:80/");


And this is my
package.json
file:

{
"name": "testapp",
"version": "1.0.0",
"description": "",
"main": "./server/index.js",
"scripts": {
"start": "node ./server/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}


That is all files of my application. If I start it up with
npm start
on localhost it works fine, and I get the "Hello world!" response as expected.

Then I made a repo on
GitHub
, and pushed this application to
GitHub
, and have verified that both files are present there.

Then I created a
Web app
in Azure, and set it to deploy from this
GitHub
repo, and it seems that the build worked just fine. This is the output from
Generating Deployment Script
:

Using cached version of deployment script (command: 'azure -y --no-dot- deployment -r "D:\home\site\repository" -o "D:\home\site\deployments\tools" --node --sitePath "D:\home\site\repository"').


And this is the output of
Running Deployment Command
:

Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling node.js deployment.
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
Copying file: 'server\index.js'
Using start-up script server/index.js from package.json.
Generated web.config.
The package.json file does not specify node.js engine version constraints.
The node.js application will run with the default node.js version 4.2.3.
Selected npm version 3.5.1
npm WARN brewingdaytest@1.0.0 No description
npm WARN brewingdaytest@1.0.0 No repository field.
Finished successfully.


And Azure says delpoyment successful.

However, when I navigate to
appname.azurewebsites.net
, it responds with a
Status Code
of
500
, and no response data at all.

What is the puzzle part I'm missing here to get this to work as expected?

Answer

Azure makes the port for your app available at process.env.PORT, so you can do something like this:

process.env.PORT = process.env.PORT || 80;

server.listen(process.env.PORT);

If the app is running on Azure, it'll use the port assigned by Azure. If it's running on localhost, it'll use 80 instead.

Comments