设置
host请求的标题:
const https = require('https');https.get('https://AA.BB.CC.DD', { headers : { host : 'api.example.com' }}, res => { console.log('okay');}).on('error', e => { console.log('E', e.message);});编辑
:我挖了一下,看看这是如何工作的。为了允许基于HTTPS的虚拟主机,有一个称为SNI(服务器名称指示)的TLS扩展。客户端使用此扩展名指示其尝试连接的主机名,因此服务器可以选择属于该主机名的适当证书。
所使用的Node
tls模块,可以
https选择
servername设置此主机名:
https.get('https://AA.BB.CC.DD', { servername : api.example.com'}, ...)但是,您仍然还需要传递
Host标头(这是常规HTTP协议的一部分):
https.get('https://AA.BB.CC.DD', { headers : { host : 'api.example.com' }, servername : 'api.example.com'}, ...)为了保持干燥,Node.js的将设置
servername为
Host标题,除非它已经设置为别的东西(在这里)。



