2015-12-08 4 views
29

Ich verwende IBM Bluemix, um einen Webdienst für ein Schulprojekt zu erstellen.Knoten js Fehler: Protokoll "https:" wird nicht unterstützt. Erwartet "http:"

Mein Projekt muss einen JSON von einer API anfordern, damit ich die bereitgestellten Daten verwenden kann. Ich verwende die http get Methode für einen Datensatz, und ich bin mir nicht sicher, ob es richtig funktioniert.

Wenn ich meinen Code ausführen, bekomme ich die Meldung:

Error: Protocol "https:" not supported. Expected "http:"

Was es verursacht, und wie kann ich sie lösen?

Hier ist meine .js Datei:

// 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"]; 
 
\t \t \t 
 
\t \t \t obj=req.query; 
 
\t \t \t 
 
\t \t \t DolarUsuario=obj['dolar']; 
 
\t \t \t RealUsuario=Number(obj['dolar'])*CotacaoDolar; 
 
\t \t \t 
 
\t \t \t EuroUsuario=obj['euro']; 
 
\t \t \t RealUsuario2=Number(obj['euro'])*CotacaoEuro; 
 
\t \t \t 
 
\t \t \t Oi=1*VariacaoDolar; 
 
\t \t \t Oi2=1*VariacaoEuro; 
 
\t \t \t 
 
\t \t \t if (VariacaoDolar<0) { 
 
\t \t \t recomend= "Recomenda-se, portanto, comprar dólares."; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t else if (VariacaoDolar=0){ 
 
\t \t \t \t recomend=""; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t else { 
 
\t \t \t \t recomend="Recomenda-se, portanto, vender dólares."; 
 
\t \t \t \t } 
 
\t \t \t \t 
 
\t \t \t if (VariacaoEuro<0) { 
 
\t \t \t recomend2= "Recomenda-se, portanto, comprar euros."; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t else if (VariacaoEuro=0){ 
 
\t \t \t \t recomend2=""; 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t recomend2="Recomenda-se,portanto, vender euros."; 
 
\t \t \t \t } \t 
 
\t \t \t \t 
 
\t \t \t res.render('cotacao_response.jade', { 
 
        \t \t 'CotacaoDolar':CotacaoDolar, 
 
\t \t \t \t \t \t 'VariacaoDolar':VariacaoDolar, 
 
\t \t \t \t \t \t 'Atualizacao':Atualizacao, 
 
\t \t \t \t \t \t 'RealUsuario':RealUsuario, 
 
\t \t \t \t \t \t 'DolarUsuario':DolarUsuario, 
 
\t \t \t \t \t \t 'CotacaoEuro':CotacaoEuro, 
 
\t \t \t \t \t \t 'VariacaoEuro':VariacaoEuro, 
 
\t \t \t \t \t \t 'RealUsuario2':RealUsuario2, 
 
\t \t \t \t \t \t 'recomend':recomend, 
 
\t \t \t \t \t \t 'recomend2':recomend2, 
 
\t \t \t \t \t \t 'Oi':Oi, 
 
\t \t \t \t \t \t 'Oi2':Oi2 
 
      }); 
 
\t \t \t 
 
\t \t 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; 
 
     }); 
 
\t \t res3.on('end', function() { 
 
      var x=json.dataset.data[0][1]; 
 
     console.log("My JSON is "+x); }); 
 
     
 
    }); 
 
    }); 
 
     }); 
 
    }); 
 
});z

Hier ist ein Druck der Fehlerbildschirm erhalte ich: enter image description here

Antwort

72

Wenn Sie eine https-Ressource anfordern möchten, müssen Sie zu verwenden https.get, nicht http.get.

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

+50

Warum wird diese Entscheidung an den Benutzer weitergegeben? Es scheint, als wäre es sinnvoll, das Protokoll einfach zu analysieren und eine geeignete Funktion hinter den Kulissen zu verwenden. –

+1

Äh, okay? Ich verstehe nicht, warum dies vom Benutzer entschieden werden soll. – BastiBen

14

Als Randnotiz für jeden eine Lösung von Google sucht ... stellen Sie sicher, Sie sind ein http.Agent mit einer HTTPS-Anforderung nicht verwenden, oder Sie werden diese Fehler erhalten.

+0

Das war es für mich, danke! – rocketmonkeys

6

Der Grund für diesen Fehler ist, dass Sie versuchen, einen HTTPS-URI von einem HTTP-Client aufzurufen. Die ideale Lösung wäre gewesen, wenn ein generisches Modul das URI-Protokoll herausfinden und die Entscheidung treffen würde, intern HTTPS oder HTTP zu verwenden.

Die Art und Weise, wie ich dieses Problem überwunden habe, besteht darin, die Schaltlogik selbst zu verwenden. Unten ist ein Code, der das Umschalten für mich getan hat.

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); 
}); 
Verwandte Themen