首页 > 解决方案 > ECONNRESET error while fetching an URL in node.js

问题描述

Getting the below error when trying to fetch (using npm node-fetch) html from the link below:

Failed to fetch page: { FetchError: request to https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ failed, reason: read ECONNRESET at ClientRequest

I am using the following snippet :

const DomParser = require("dom-parser");
const parser = new DomParser();
const fetch = require("node-fetch");

router.get("/info", (req, res, next) => {
  var url =
    "https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ";
  fetch(url)
    .then(function(response) {
      // When the page is loaded convert it to text
      return response.text();
    })
    .then(function(html) {
      // Initialize the DOM parser

      // Parse the text
      var doc = parser.parseFromString(html, "text/html");

      // You can now even select part of that html as you would in the regular DOM
      // Example:
      // var docArticle = doc.querySelector('article').innerHTML;

      console.log(doc);
    })
    .catch(function(err) {
      console.log("Failed to fetch page: ", err);
    });
});

The response was consoled log few times before showing the error and now its throwing err everytime I call /info.

I have tried the snippet in repl online editor. it returns Promise {pending}.

标签: node.jsexpressdomfetch

解决方案


I would use some modern promise based packages to do this job. some are got, axios etc. node-fetch was last published 8 months ago. It might not be able to handle the encoding or compressing.

here is an example using axios which works.

const axios = require("axios");
const DomParser = require("dom-parser");
const parser = new DomParser();

var url =
  "https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ";
axios(url)
  .then(response => response.data)
  .then(html => {
    // Initialize the DOM parser

    // Parse the text
    var doc = parser.parseFromString(html, "text/html");

    // You can now even select part of that html as you would in the regular DOM
    // Example:
    // var docArticle = doc.querySelector('article').innerHTML;

    console.log(doc);
  })
  .catch(function(err) {
    console.log("Failed to fetch page: ", err);
  });


推荐阅读