Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node server running but localhost refusing to connect

Trying to get the simplest node server interacting with the browser, with 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 won't do it..

localhost refused to connect

Thats the IPv4 address. Am I missing something here?

like image 254
godhar Avatar asked Feb 16 '17 20:02

godhar


People also ask

Why is 127.0 0.1 refused to connect?

0.1 (loopback address). So your client is trying to connect to any of the non-loopback addresses of your machine, while your server is listening only on the loopback address . So, no connection can be established. The solution to this problem is that connect to the same end point your server is listening on.

Why is node js not working?

Problems occurring in Node. js application deployments can have a range of symptoms, but can generally be categorized into the following: Uncaught exception or error event in JavaScript code. Excessive memory usage, which may result in an out-of-memory error.


2 Answers

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

like image 176
vladwoguer Avatar answered Oct 13 '22 00:10

vladwoguer


Had the same problem and solved it like this:

var http = require('http');

http.createServer(function(req, res){

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

}).listen(1337);
  1. On node Terminal: node filename.js

    Leave it open, don't CTRL + C.

  2. Go to webpage: localhost:1337

It should work. The problem is not closing the terminal or exiting the execution of the node File.

like image 40
Kela Avatar answered Oct 13 '22 00:10

Kela