Pranav Nemade Pranav Nemade - 1 month ago 15
HTTP Question

Node JS not listening to port 1337 on server

I'm trying to open a port on particular lamp server hosted by Google and I'm in connection with the server via ssh.

I've followed this link to configure nvm and the latest Node JS(v0.12.5) on it. After installing, I've used this demo code in "server.js" file and using the command "node server.js", it looks like Node JS is running, giving this message "Server ready" at the server console. Now the problem is that when I check for the open port using "netstat -n", I dont see any 1337 port open, which it should be. I've also tried to connect through browser using "serverIPaddress:1337", but I get "Conecting..." message and then nothing happens.

Any idea where I'm messing up??
I'm also confused with the server IP address(localhost:127.0.0.1) or (globalIPaddress) to put in the server.js file.

P.S: Please find the server.js file script below.



var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server ready');




Answer

You have two problems:

1) "127,0,0,1" means "localhost" - it is NOT appropriate if you want remote clients to connect.

2) port 1337 may (or may not) be open through a firewall. It sounds like it isn't.

SUGGESTED CHANGE:

var http = require('http');
http.createServer(function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.end('Hello World\n');
}).listen(80);
console.log('Server ready');

This is assuming that your remote server doesn't have some OTHER web server already bound to port 80. If your revised program dies with a "port in use" error, then try port 8080. Or port 8888.

Comments