2013-08-03 5 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/'); 

तो, अगर मैं 1 9 2.168.1.100 को सुनना चाहता हूं, तो बस इसी तरह?नोडजेज़ का उपयोग कैसे करें एकाधिक 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'); 

उत्तर

4

इस

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 सॉकेट बनाता है के रूप में, आप एक सॉकेट से आईपीएस की सूची असाइन नहीं कर सकते, इसलिए आप हर आईपी के लिए अलग http वस्तुओं को बनाने के लिए, या का उपयोग 0.0.0.0 (या बस न दूसरा पैरामीटर को परिभाषित) सुनने के लिए करने की आवश्यकता है सभी उपलब्ध ips पर।

संबंधित मुद्दे