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

Więc jeśli chcę słuchać 192.168.1.100, tak jak to?jak korzystać z nodejs słuchać wielu ips?

var http = require('http'); 
http.createServer(function (req, res) { 
res.writeHead(200, {'Content-Type': 'text/plain'}); 
res.end('Hello World\n'); 
}).listen(80, '127.0.0.1').listen(80,'192.168.1.100'); 

Odpowiedz

4

Spróbuj

var http = require('http'); 
function handler(req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}; 
http.createServer(handler).listen(80, '127.0.0.1'); 
http.createServer(handler).listen(80, '192.168.1.100'); 
// or listen to both instead - thx @FlashThunder 
// http.createServer(handler).listen(80); 
5

http tworzy gniazdo, nie można przypisać listę adresów IP do jednego gniazda, dlatego trzeba utworzyć oddzielne obiekty http dla każdego adresu IP lub użyj 0.0.0.0 (lub po prostu nie definiują drugi parametr), aby słuchać na wszystkich dostępnych ips.

Powiązane problemy