【发布时间】:2019-02-13 07:05:34
【问题描述】:
我正在编写一个简短的 Node.js sn-p,它解析 RSS 提要、提取链接、将它们重新配置为我想要的 PDF 链接,然后写入这些文件。代码如下所示:
var https = require('https');
var fs = require('fs');
const Parser = require("rss-parser");
let parser = new Parser();
parser.parseURL("https://regulations.justia.com/regulations/fedreg?limit=20&mode=atom")
.then((feed) => {
const base = "https://docs.regulations.justia.com/entries"
feed.items.forEach((item, i) => {
// Parsing to create PDF link...
const str = item.link;
let dates = str.substring(50, 60);
let newDates = dates.replace(/\//, "-").replace(/\//, "-");
let ending = str.substring(61).replace(".html",".pdf");
let fullString = `${base}/${newDates}/${ending}`;
// Fetching and saving the PDF file....
const file = fs.createWriteStream(`${item.title}.pdf`);
const request = https.get(fullString, (res) => {
res.pipe(file);
});
});
})
.catch((err) => console.log(err));
我现在遇到两个错误。
1) 与我的可写流有关。当我尝试根据 RSS 提要中的item.title 创建文件时,每次都会收到此错误:
Error: ENOENT: no such file or directory, open 'Notice - Solicitation of Nominations for Appointment to the World Trade Center Health Program Scientific/Technical Advisory Committee (STAC).pdf'
这是否与项目标题中的括号或破折号有关?如果不是,还有什么可能导致此问题?
2) 当我更改代码(将可写流命名为更简单的名称)时,我的代码将抛出以下错误:
Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1135:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
这个错误通常是在我下载了一些 PDF 之后引发的,但不是全部。在这个例子中我可以改变什么来克服这些错误?感谢您的帮助!
【问题讨论】:
标签: javascript node.js sockets stream rss