0

I have a js file with the following code

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

Now, if I access the server by 127.0.0.1:3000 its perfectly fine, but I want to access it from my own computer ip address. I write 192.xxx.x.xxx:3000, but I cant access it. Since I am developing an android application I need the ip address of the computer in order to run it, can someone explain why I am unable to access it?

Joseph
  • 69
  • 4
  • 13
  • The `127.0.0.0/8` is a loopback address block. – hexacyanide Sep 13 '13 at 18:53
  • @Joseph "*Loopback*" means "*this machine*." And, any IP addresses you specify to listen to are the only IP addresses the application can respond to. And, quite simply, `127.0.0.1 != 192.x.x.x`. To respond to requests using multiple IPs, you have to `.listen()` to each of them. Or `.listen()` to [`IPADDR_ANY`](http://en.wikipedia.org/wiki/0.0.0.0) to respond to any IPv4 address by leaving off the `hostname` -- `.listen(3000)`. – Jonathan Lonowski Sep 13 '13 at 19:34
  • Related: http://stackoverflow.com/q/7208062, http://stackoverflow.com/q/13431974, http://stackoverflow.com/q/18678607. Reference: [`server.listen(port, [hostname], ...)`](http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback) – Jonathan Lonowski Sep 13 '13 at 19:41

1 Answers1

4

When you say listen(3000, '127.0.0.1'), you're explicitly binding your server to port 3000 on IP 127.0.0.1.

You probably just want to bind to all IPs, which you can do by omitting the bind host:

listen(3000);
josh3736
  • 139,160
  • 33
  • 216
  • 263