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 Failed, Reason...
Getting the below error when trying to fetch (using npm node-fetch) html from the link below:
Failed to fetch page: { FetchError: request to 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 =
"";
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}.
2 Answers
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 =
"";
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);
});
As far as I know you cannot use fetch in NodeJS. You need to rely in different methods. I personally usually use Axios.
Check also if this part of the code actually returns a promise:
return response.text()
Before chaining another .then.