2

I set up a Node JS server, and made a request to it, it just loads and loads and eventually says "Server not found". Here is the code for my file:

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/');

When going to externalIP:1337, the phenomenon described above happens. I am running Ubuntu 14.04, node JS version 0.10.32. What is going on?

1
  • made a request to it, how are you making a request?!? Commented Oct 4, 2014 at 14:11

3 Answers 3

3

You're specifically listening to 127.0.0.1 which is localhost. If you want to allow connection via the external IP, you should omit the '127.0.0.1' argument in your listen. i.e. change listen(1337, '127.0.0.1') to listen(1337). Otherwise go to localhost:1337 instead.

Sign up to request clarification or add additional context in comments.

4 Comments

I can't access localhost:1337. I am working through an ssh connection.
Is this on a VM on the cloud somewhere? Some cloud providers like AWS have firewalls that only allow certain ports.
I am working remotely on a server at my house. I am an admin on the server. I believe the problem is not a firewall issue since the same happens when port is 80. I also think this is an issue with Node since Tomcat and Apache work fine.
Try running netstat -an | grep 1337 to see if port 1337 is open
3

The problem is that you're only listening for requests on localhost. If you try to access the server from outside the system you won't get there because the server isn't listening on a LAN IP.

Change

.listen(1337, '127.0.0.1');

to

.listen(1337);

That will listen on all available network interfaces on the system. You could specify a LAN IP (just like you did for localhost) if you wanted to listen on a specific network interface.

2 Comments

Have you checked the firewall this server is running on?
Yes. The firewall is OK.
0

Sorry. Apparently tomcat was also using port 80. So by disabling tomcat I got it to work. Thanks.

1 Comment

Typically you get an error when you try to bind to a port that's already in use. Were you not getting some kind of error when starting the server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.