configuration node.js windows 2008 server

2019-08-29 02:23发布

问题:

I did install all needed package and node.js to dedicated machine Windows 2008 Server.

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

So when I call http://local.host:1337/, I get 'Hello World' But if try to call this service from another machine: http://my.domain.ip.address:1337/ Ooops, I can't see nothing. I already switch Off firewall at all

Thanks, to All advices

回答1:

Listening to localhost or 127.0.0.1 only allows for responding to requests made from the same computer to that specific IP or hostname.

To have your application respond to requests for multiple IP addresses, you'll need to listen to each of them. You can either do this individually.

function server(req, res) {
    // ...
}

http.createServer(server).listen(port, '127.0.0.1');
http.createServer(server).listen(port, 'my.domain.ip.address');
http.createServer(server).listen(port, '<any other public facing IP address>');

Or, you can listen to IPADDR_ANY (0.0.0.0), which in a non-specific, meta address. And, this is the default value for the hostname argument, so you only need to specify the port.

http.createServer(function (req, res) {
    // ...
}).listen(port);