node.js - 发出 http 或 https 请求时如何指定 IP 和主机名?

标签 node.js

https.request({
  ip: '127.0.0.1',
  hostname:'internal.hostname_required_for_certificate.com',
  method: 'GET',
  path: '/'
})

以上代码不起作用,这意味着我需要在发送请求时向 127.0.0.1 发送请求 https://internal.hostname_required_for_certificate.com/...

最佳答案

您可以手动指定Host标题:

const req = https.request({
    host: '127.0.0.1',
    method: 'GET',
    path: '/',
    headers: {
        'Host': 'internal.hostname_required_for_certificate.com'
    }
}, (res) => {
    res.setEncoding('utf8');
    res.pipe(process.stdout);
});

req.end();

关于node.js - 发出 http 或 https 请求时如何指定 IP 和主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54018673/

相关文章:

css - grunt-uncss 创建重复的选择器

node.js - 如何防止 Node 在我的 NextJS/Express 应用程序中将异常记录到控制台?

javascript - Heroku nodejs 服务器出现 EADDRNOTAVAIL 错误

javascript - 销毁 Node 中的请求

node.js - docker-compose ECONNREFUSED 用于 nodeJS 上的 Postgres

node.js - 使用 mongoose 和 typescript 创建模式

linux - 在多个 node.js HTTP 进程之间共享一个端口

javascript - 根据数组对象中存在的键排序

javascript - Nodejs应用程序中身份验证重定向的无限循环

node.js - Safari 和 iOS 中的 Express.js 中的 Gzip 压缩问题