简体   繁体   English

node.js TypeError [ERR_INVALID_PROTOCOL]:不支持协议“http:”。 预计“https:

[英]node.js TypeError [ERR_INVALID_PROTOCOL]: Protocol “http:” not supported. Expected "https:

Given below Node.js code i was try to use https as the project required but getting error下面给出 Node.js 代码,我尝试使用 https 作为项目所需,但出现错误

TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. TypeError [ERR_INVALID_PROTOCOL]:不支持协议“http:”。 Expected "https:"预期“https:”

const express = require("express");
var https = require("https");


const app = express();


app.get("/", function(req, res) {

    const url = "http://api.openweathermap.org/data/2.5/weather?q=Gwalior&appid=4b01458a2b74d57ff37f136f382ac4d5&units=metric";

    https.get(url, function(response) {
        console.log(response.statusCode)
    });
    res.send("Server is Up and Running");
})



app.listen(3000, function() {
    console.log("Server is running on port 3000.");
})

Complete Error stack trace:完整的错误堆栈跟踪:

TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
    at new ClientRequest (_http_client.js:151:11)
    at request (https.js:316:10)
    at Object.get (https.js:320:15)
    at C:\Users\Public\Documents\WeatherProject\app.js:12:10
    at Layer.handle [as handle_request] (C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Public\Documents\WeatherProject\node_modules\express\lib\router\index.js:335:12)

But if i am using http its working fine even i have tried with node install https (still not work)但是,如果我使用 http 它工作正常,即使我已经尝试使用节点安装 https (仍然无法正常工作)

Your doing https request and your url starts with http:// when it should be https:// .你做的 https 请求和你的 url 以http://开头,而它应该是https://

The problem with the code is that you are making https request and in your url you are using "http" instead of "https" .代码的问题是您正在发出 https 请求,并且在您的 url 中您使用的是"http"而不是"https" So if you change your url to "https" it should work.因此,如果您将 url 更改为"https" ,它应该可以工作。

https://api.openweathermap.org/data/2.5/weather?q=Gwalior&appid=4b01458a2b74d57ff37f136f382ac4d5&units=metric

I know you have figured this out but for the purpose of those that might run into this in the future - So the code should be:我知道您已经解决了这个问题,但是出于将来可能会遇到此问题的目的-因此代码应该是:

const express = require("express");
var https = require("https");


const app = express();


app.get("/", function(req, res) {

    const url = "https://api.openweathermap.org/data/2.5/weather?q=Gwalior&appid=4b01458a2b74d57ff37f136f382ac4d5&units=metric";

    https.get(url, function(response) {
        console.log(response.statusCode)
    });
    res.send("Server is Up and Running");
})



app.listen(3000, function() {
    console.log("Server is running on port 3000.");
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM