8
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/');

So,if I wanna listen to 192.168.1.100,just like this?

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');

2 Answers 2

9

Try this

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); 
Sign up to request clarification or add additional context in comments.

Comments

8

As http creates socket, you can't assign list of ips to one socket, that's why you need to create separate http objects for every ip, or use 0.0.0.0 (or simply dont define second parameter) to listen on all available ips.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.