【问题标题】:Make https request from Node js http server从 Node js http 服务器发出 https 请求
【发布时间】:2015-12-18 07:14:29
【问题描述】:

我的实时网站是https://example.com。现在我在http://example.com:8081 上创建了一个node js 服务器。我有以下代码:

var options = {
    host: 'https://example.com',
    port: 443,
    path: '/scanner/scoutdata',
    method: 'GET'
};
and sending request like this:
http.request(options, function(res){
  //my code here.
});

现在我在 linux 服务器上运行此脚本时收到 302 错误消息。 我不能从同一域下的 http:// 节点服务器调用带有 https:// 的 url 吗?我应该在 https:// 中创建我的节点 js 服务器以使用 https://

调用 url

【问题讨论】:

    标签: javascript node.js express https socket.io


    【解决方案1】:

    如果在端口 443(SSL 端口)上联系服务器,请使用 https.get() 而不是 http.get()

    302 响应只是告诉您端口和协议不匹配,它希望它们匹配。因此,在端口 80 上使用 http,在端口 443 上使用 https。

    【讨论】:

      【解决方案2】:

      你可以使用 axios

      var axios = require('axios');
      
      axios.get("https://example.com:443/scanner/scoutdata").then(function (response) {
          console.log(response.data);
        });
      
      

      【讨论】:

        【解决方案3】:

        请尝试使用请求模块,因为我将代码放在下面-

        request("https://example.com:443/scanner/scoutdata", function(error, response, body) { 
               console.log(body);
        });
        

        谢谢

        【讨论】:

          猜你喜欢
          • 2014-06-29
          • 2018-01-14
          • 2021-10-10
          • 1970-01-01
          • 2012-10-18
          • 2017-05-05
          • 1970-01-01
          • 2020-07-15
          相关资源
          最近更新 更多