BRampersad
BRampersad

Reputation: 862

Node.JS Not working on the internet

i have the basic webserver hello world app for nodejs on windows and it works on localhost. But when i test it from the internet it cannot connect. I set up port forwarding in my netgear router. Am i missing a step here to make my nodejs server visible to the outside world?

Thanks.

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 running at http://127.0.0.1:1337/');

Upvotes: 11

Views: 9195

Answers (3)

Ygautomo
Ygautomo

Reputation: 874

Just to make sure.

Your code should run like this.

var http = require('http');
const port = 1337;
const host = '0.0.0.0';

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port, host);
console.log('Server running at http://${host}:${port}');

Upvotes: 0

Bryan B
Bryan B

Reputation: 4535

Make sure you listen on 0.0.0.0 instead of 127.0.0.1

127.0.0.1 is a private network visible only to your computer. 0.0.0.0listens to all interfaces, including both the private and public (as public as it can be behind a NAT).

Upvotes: 18

goatslacker
goatslacker

Reputation: 10190

Looks like you're binding the server to IP Address 127.0.0.1 which is localhost. If you want to access it elsewhere you'll need to set it to it's internet IP. Check out whatismyip.com and use that IP instead.

Upvotes: 0

Related Questions