gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:26:29 27 4
gpt4 key购买 nike

我拥有两个域,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 查看页面。

那么 主机名参数有什么作用呢?

最佳答案

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

// 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,所以两者都可以工作。

为了让我做共享主机,我必须找到另一种方式。

关于node.js::hostname 在 `listen` 函数中做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3173091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com