Node server is running, but localhost refuses to connect

Trying to get the simplest node server interacting with the browser using this:

var http = require('http');


http.createServer(function(req, res){

  res.writeHead( 200, { "content-Type" : 'text/plain' } )
  res.send('Hello world');

}).listen(1337, '192.168.1.2');

but localhost will not do this.

localhost refused to connect

This is an IPv4 address. Am I missing something?

+6
source share
6 answers

Use 0.0.0.0 and it will work both "192.168.1.2" and localhost.

+4
source

I think you only have a typo, use res.endto send data and close the connection instead res.send.

In addition, you should not have problems connecting to localhost

var http = require('http');


http.createServer(function(req, res){

  res.writeHead( 200, { "content-Type" : 'text/plain' } )
  res.end('Hello world');

}).listen(1337, 'localhost');
+1
source

localhost, "localhost" .

0

IP-, . IP-.

0

- CMD.exe.

0

, Mac. 192.168.1.2 127.0.0.1 0.0.0.0, .

http://127.0.0.1:1337/, http://localhost:1337/. - localhost ::1 - IPv6 Mac. - Node IPv6 .

0

Source: https://habr.com/ru/post/1669983/


All Articles