简体   繁体   English

NodeJS:Https 请求错误:连接 ECONNREFUSED 127.0.0.1:443

[英]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.

相关问题 Node.js-https.request错误:连接ECONNREFUSED - Nodejs - https.request Error: connect ECONNREFUSED https.request() 给出错误:连接 ECONREFUSED 127.0.0.1:443 - https.request() is giving Error: connect ECONREFUSED 127.0.0.1:443 PreSignUP 在 AWS Lambda function 中出现错误连接 ECONNREFUSED 127.0.0.1:443 失败 - PreSignUP failed with error connect ECONNREFUSED 127.0.0.1:443 in AWS Lambda function Fluentd Kubernetes Nodejs:错误:连接ECONNREFUSED 127.0.0.1:24224 - Fluentd Kubernetes Nodejs : Error: connect ECONNREFUSED 127.0.0.1:24224 NodeJS - 错误:连接 ECONNREFUSED 127.0.0.1:port(chrome-remote-interface) - NodeJS - Error: connect ECONNREFUSED 127.0.0.1:port (chrome-remote-interface) [已解决]错误:连接 ECONNREFUSED 127.0.0.1:587 nodemailer NodeJS - [SOLVED]Error: connect ECONNREFUSED 127.0.0.1:587 nodemailer NodeJS 错误:连接ECONNREFUSED 127.0.0.1:3002 - Error: connect ECONNREFUSED 127.0.0.1:3002 错误:连接ECONNREFUSED 127.0.0.1:3100 - Error: connect ECONNREFUSED 127.0.0.1:3100 Docker NodeJS 与 MySQL 连接 ECONNREFUSED 127.0.0.1:8000 - Docker NodeJS with MySQL connect ECONNREFUSED 127.0.0.1:8000 使用 Nodejs 连接 ECONNREFUSED 127.0.0.1:3000 - DB Mongoose - connect ECONNREFUSED 127.0.0.1:3000 - DB Mongoose with Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM