简体   繁体   中英

Node JS - Server doesn't react to requests

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?

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. ie change listen(1337, '127.0.0.1') to listen(1337) . Otherwise go to localhost:1337 instead.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM