node.js :: hostname在`listen`函数中做了什么?

我拥有两个域名,abc.com和xyz.com(不是我所拥有的真实域名,但是它们只是一个例子)。 他们都指向相同的IP地址。 以下是我的服务器js文件:

var sys=require('sys'), http=require('http'), settings=require('./settings'); var srv = http.createServer(function(req, res) { var body="<b>Hello World!</b>" res.writeHead(200, { 'content-length': body.length, 'content-type': 'text/html', 'stream': 'keep-alive', 'accept': '*/*' } ); res.end(body); }); srv.listen(8000, 'abc.com' ); // (settings.port, settings.hostname); 

然后我访问http://abc.com:8000/和http://xyz.com:8000/ ,他们都显示网页。 我以为我只能在abc.com上看到这个页面,因为这就是我设置的主机名。

但是,当我把“127.0.0.1”作为主机名时,我只能通过服务器上的wget来查看页面。

那么hostname参数是做什么的?

net.js中定义listen函数的以下代码片段是相关的:

 // the first argument is the port, the second an IP var port = arguments[0]; dns.lookup(arguments[1], function (err, ip, addressType) { if (err) { self.emit('error', err); } else { self.type = addressType == 4 ? 'tcp4' : 'tcp6'; self.fd = socket(self.type); bind(self.fd, port, ip); self._doListen(); } }); 

所以基本上提供一个url作为主机名参数不允许共享主机。 所有的node.js都是为你解决一个主机名到一个IP地址的工作 – 因为在我的情况下,两个域指向相同的IP,都将工作。

对于我做共享主机,我必须find另一种方式。