node.js - 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}
.
解决方案
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);
});
推荐阅读
- javascript - 如何在Javascript中修改或替换对象数组中的键
- android - perform(click()) 在 Android 11 中不起作用,但在 Android 10 中起作用
- c# - Unity3D - 无法在运行时正确设置纹理
- websphere - 在 IBM BPM 中找不到数据源
- linux - Cmake vcpkg 和 Bullet
- linux - wsl 中的 traceroute 命令不起作用。outpot 是 ***
- clojurescript - 将 reframe 订阅中的参数传递给链函数 clojurescript
- python - 沿轴之一应用 numpy 'where'
- excel - 尝试将数据从网站检索到 Excel 时出现问题
- amazon-web-services - 通过 ssm 文档将参数传输到 Lambda