[英]Node js Error: Protocol "https:" not supported. Expected "http:"
[英]node.js TypeError [ERR_INVALID_PROTOCOL]: Protocol “http:” not supported. Expected "https:
下面给出 Node.js 代码,我尝试使用 https 作为项目所需,但出现错误
TypeError [ERR_INVALID_PROTOCOL]:不支持协议“http:”。 预期“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.");
})
完整的错误堆栈跟踪:
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)
但是,如果我使用 http 它工作正常,即使我已经尝试使用节点安装 https (仍然无法正常工作)
你做的 https 请求和你的 url 以http://
开头,而它应该是https://
代码的问题是您正在发出 https 请求,并且在您的 url 中您使用的是"http"
而不是"https"
。 因此,如果您将 url 更改为"https"
,它应该可以工作。
https://api.openweathermap.org/data/2.5/weather?q=Gwalior&appid=4b01458a2b74d57ff37f136f382ac4d5&units=metric
我知道您已经解决了这个问题,但是出于将来可能会遇到此问题的目的-因此代码应该是:
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.