-1

While using http.createServer() method on Node.js, I am unable to use any other address than '127.0.0.1'. My understanding is that I should be able to use any address under Class A IPv4 addresses, but it is not true as I am thrown an error.

Can someone kindly help me understand this?

Terminal error log:

enter image description here

Code:

const http = require('http');

const hostname = '127.0.0.1';

const port = 1337;

http.createServer((request, response) => {

  response.writeHead(200, { 'Content-Type': 'text/plain' });

  response.end('Hello World\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60

1 Answers1

1

You can only use addresses assigned to one of your network interfaces. 127.0.0.0 isn't assigned to any of your network interfaces (and it can't be). The localhost addresses are 127.0.01 through 127.255.255.254; 127.0.0.0 and 127.255.255.255 are network / broadcast addresses (or so this comment says on superuser).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875