问题
I'm trying to open a port on particular lamp server hosted by Google and I'm in connection with the server via ssh.
I've followed this link to configure nvm and the latest Node JS(v0.12.5) on it. After installing, I've used this demo code in "server.js" file and using the command "node server.js", it looks like Node JS is running, giving this message "Server ready" at the server console. Now the problem is that when I check for the open port using "netstat -n", I dont see any 1337 port open, which it should be. I've also tried to connect through browser using "serverIPaddress:1337", but I get "Conecting..." message and then nothing happens.
Any idea where I'm messing up?? I'm also confused with the server IP address(localhost:127.0.0.1) or (globalIPaddress) to put in the server.js file.
P.S: Please find the server.js file script below.
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 ready');
回答1:
You have two problems:
1) "127,0,0,1" means "localhost" - it is NOT appropriate if you want remote clients to connect.
2) port 1337 may (or may not) be open through a firewall. It sounds like it isn't.
SUGGESTED CHANGE:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World\n');
}).listen(80);
console.log('Server ready');
This is assuming that your remote server doesn't have some OTHER web server already bound to port 80. If your revised program dies with a "port in use" error, then try port 8080. Or port 8888.
回答2:
Try removing '127.0.0.1' or change it to 0.0.0.0
- to listen on all interfaces.
See documentation for details
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
With current settings the server accepts connection only from localhost. Also you need to tune firewall to open the 1337 port on remote server
回答3:
Just put port number to listen on rather than using both port number and ip-adress
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World\n');
}).listen(1337);
console.log('Server ready');
if you want to use ip adress than the server will be accessible only from local with url 127.0.0.1:1337 you will not be able to access it with localhost:1337
回答4:
Yes, I've tried several times to send to the server, but sometimes it doesn't work - If you use :
"lsof -P -iTCP | grep LISTEN"
you could check the process in -bash.
And then determinate how many ports are working. You can kill the process using :
"kill -9 process_id".
"port in use" means that port is being used by other applications or program is running on it.
Like example:
iTunes some times don't let you send in the same port.
For that is better Quit the app and try to send it later.
来源:https://stackoverflow.com/questions/31152460/node-js-not-listening-to-port-1337-on-server