Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 634
Pranav Nemade Avatar asked Jan 09 '23 01:01

Pranav Nemade


1 Answers

Try removing '127.0.0.1' or change it to 0.0.0.0 - to listen on all interfaces. See documentation for details https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback

With current settings the server accepts connection only from localhost. Also you need to tune firewall to open the 1337 port on remote server

like image 107
vodolaz095 Avatar answered Jan 10 '23 18:01

vodolaz095