本文介绍了节点js错误:协议“https:”不支持。预期的“http:”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using IBM Bluemix to make a web service for a school project.

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

My project needs to request a JSON from an API, so I can use the data it provides. I use the http get method for a data set, and I am not sure if it is working properly.

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

When I run my code, I get the message:

造成它的原因是什么以及如何解决?

What is causing it and how can I solve it?

这是我的 .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); });

    });
    });
        });
    });
});

这是我得到的错误屏幕的打印:

Here is a print of the error screen I get:

推荐答案

如果要申请https资源,则需要使用 https.get ,而不是 http.get

When you want to request an https resource, you need to use https.get, not http.get.

这篇关于节点js错误:协议“https:”不支持。预期的“http:”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:28