[英]NodeJS: Https Request Error: connect ECONNREFUSED 127.0.0.1:443
So I want to load an image from an external URL.所以我想从外部 URL 加载图像。
var https = require('node:https');
export const loadImageFromUrl = (url: string) => {
return new Promise((resolve, reject) => {
try {
https.request(url, function (response: any) {
resolve(response);
});
} catch (err) {
reject(err);
}
});
};
For example, the image is from https://en.pimg.jp/054/313/779/1/54313779.jpg
.例如,图像来自https://en.pimg.jp/054/313/779/1/54313779.jpg
。 When I run the code, it gives me this error:当我运行代码时,它给了我这个错误:
Error: connect ECONNREFUSED 127.0.0.1:443错误:连接 ECONNREFUSED 127.0.0.1:443
This is weird, the URL is not from my local computer, why does https
search from my local address?这就奇怪了,网址不是我本地电脑的,为什么https
是从我本地地址搜索的?
You can try this code.你可以试试这个代码。 It should work.它应该工作。
You can have a look at the live demo here您可以在此处查看现场演示
const https = require("https");
const { URL } = require("url");
const loadImageFromUrl = (url) => {
const urlParams = new URL(url);
const hostname = urlParams.hostname;
const path = urlParams.pathname;
const options = {
hostname: hostname,
port: 443,
path: path,
method: "GET",
};
const request = https.request(options, (res) => {
res.on("data", (data) => {
console.log(`response data: ${data}`);
});
});
request.on("error", (error) => {
console.error(`Error on Get Request --> ${error}`);
});
request.end();
};
loadImageFromUrl("https://en.pimg.jp/054/313/779/1/54313779.jpg");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.