0

I have an azure website which has a URL of something like : http://app.azurewebsites.net/. Now I have my server.js file that contains the following code:

var restify = require('restify');

function respond(req, res, next) {
  res.send('hello ' + req.params.name);
  next();
}

var server = restify.createServer();
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

I have already committed my node_modules folder which contains the restify dependency. Now, when I hit http://app.azurewebsites.net:8080/hello/john I get no response back and query just times out !

When I hit http://app.azurewebsites.net/ in the debug window I see:

2015-01-22T16:53:27  Welcome, you are now connected to log-streaming service.
restify listening at http://0.0.0.0:8080

I am stuck and not sure why I am not getting "hello john" as a response in my browser. Can someone help me out here?

tsure
  • 315
  • 3
  • 6
  • 16

2 Answers2

2

This is probably because you have explicitly set your port to 8080.

Do this instead:

var restify = require('restify');
var port = process.env.PORT || 8080;

function respond(req, res, next) {
  res.send('hello ' + req.params.name);
  next();
}

var server = restify.createServer();
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);

server.listen(port, function() {
  console.log('%s listening at %s', server.name, server.url);
});

Note that i have added a port variable and used it with server.listen(port, ...

ctv
  • 1,061
  • 9
  • 11
  • Why would explicitly setting the port make a difference to using a variable (assuming the same value was set)? – infojolt Feb 25 '18 at 12:50
  • 1
    This was a while ago. From what I can remember Azure will decide what the port is for you. So the variable port gets set process.env.PORT when running in Azure and it becomes 8080 when you run locally (if you dont set the env variable yourself) – ctv Feb 28 '18 at 01:05
1

I faced the same issue when tried to listen on particular port.

var port = process.env.PORT || 3000;

Added the port variable and consumed it in the listen method. It solved the problem.