节点js错误:协议“https:”不支持。 预计“http:”

我正在使用IBM Bluemix为学校项目制作Web服务。

我的项目需要从API请求一个JSON,所以我可以使用它提供的数据。 我使用http get方法的数据集,我不知道它是否正常工作。

当我运行我的代码,我得到的消息:

错误:协议“https:”不受支持。 预计“http:”

是什么原因造成的,我该如何解决?

这是我的.js文件:

 // Hello. // // This is JSHint, a tool that helps to detect errors and potential // problems in your JavaScript code. // // To start, simply enter some JavaScript anywhere on this page. Your // report will appear on the right side. // // Additionally, you can toggle specific options in the Configure // menu. function main() { return 'Hello, World!'; } main();/*eslint-env node*/ //------------------------------------------------------------------------------ // node.js starter application for Bluemix //------------------------------------------------------------------------------ // HTTP request - duas alternativas var http = require('http'); var request = require('request'); // cfenv provides access to your Cloud Foundry environment // for more info, see: https://www.npmjs.com/package/cfenv var cfenv = require('cfenv'); //chama o express, que abre o servidor var express = require('express'); // create a new express server var app = express(); // serve the files out of ./public as our main files app.use(express.static(__dirname + '/public')); // get the app environment from Cloud Foundry var appEnv = cfenv.getAppEnv(); // start server on the specified port and binding host app.listen(appEnv.port, '0.0.0.0', function() { // print a message when the server starts listening console.log("server starting on " + appEnv.url); }); app.get('/home1', function (req,res) { http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) { var body = ''; res2.on('data', function (chunk) { body += chunk; }); res2.on('end', function () { var json = JSON.parse(body); var CotacaoDolar = json["dolar"]["cotacao"]; var VariacaoDolar = json["dolar"]["variacao"]; var CotacaoEuro = json["euro"]["cotacao"]; var VariacaoEuro = json["euro"]["variacao"]; var Atualizacao = json["atualizacao"]; obj=req.query; DolarUsuario=obj['dolar']; RealUsuario=Number(obj['dolar'])*CotacaoDolar; EuroUsuario=obj['euro']; RealUsuario2=Number(obj['euro'])*CotacaoEuro; Oi=1*VariacaoDolar; Oi2=1*VariacaoEuro; if (VariacaoDolar<0) { recomend= "Recomenda-se, portanto, comprar dólares."; } else if (VariacaoDolar=0){ recomend=""; } else { recomend="Recomenda-se, portanto, vender dólares."; } if (VariacaoEuro<0) { recomend2= "Recomenda-se, portanto, comprar euros."; } else if (VariacaoEuro=0){ recomend2=""; } else { recomend2="Recomenda-se,portanto, vender euros."; } res.render('cotacao_response.jade', { 'CotacaoDolar':CotacaoDolar, 'VariacaoDolar':VariacaoDolar, 'Atualizacao':Atualizacao, 'RealUsuario':RealUsuario, 'DolarUsuario':DolarUsuario, 'CotacaoEuro':CotacaoEuro, 'VariacaoEuro':VariacaoEuro, 'RealUsuario2':RealUsuario2, 'recomend':recomend, 'recomend2':recomend2, 'Oi':Oi, 'Oi2':Oi2 }); app.get('/home2', function (req,res) { http.get('https://www.quandl.com/api/v3/datasets/BCB/432.json?api_key=d1HxqKq2esLRKDmZSHR2', function (res3) { var body = ''; res3.on('data', function (chunk) { body += chunk; }); res3.on('end', function () { var x=json.dataset.data[0][1]; console.log("My JSON is "+x); }); }); }); }); }); });z 

这是我得到的错误屏幕的打印: 在这里输入图像描述

       

网上收集的解决方案 "节点js错误:协议“https:”不支持。 预计“http:”"

当你想请求https资源时,你需要使用https.get ,而不是http.get

https://nodejs.org/api/https.html

对于任何正在寻找Google解决scheme的人来说,请确保您没有使用带有https请求的http.Agent,否则会出现此错误。

出现此错误的原因是您正在尝试从HTTP客户端调用HTTPS URI。 理想的解决scheme是通用模块找出URI协议,并决定在内部使用HTTPS或HTTP。

我克服这个问题的方法是使用我自己的开关逻辑。 下面是一些代码,为我做了切换。

 var http = require('http'); var https = require('https'); // Setting http to be the default client to retrieve the URI. var url = new URL("https://www.google.com") var client = http; // You can use url.protocol as well if (url.toString().indexOf("https") === 0){ client = https; } // Now the client is loaded with the correct Client to retrieve the URI. var req = client.get(url, function(res){ // Do what you wanted to do with the response 'res'. console.log(res); });