[英]Writing Multiple Files From Node.js Stream
I'm writing a short Node.js snippet that parses an RSS feed, extracts the links, reconfigures them to the PDF links that I want, and then writes those files. 我正在编写一个简短的Node.js代码段,用于分析RSS提要,提取链接,将其重新配置为所需的PDF链接,然后编写这些文件。 The code looks like this:
代码如下:
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));
I'm experiencing two errors right now. 我现在遇到两个错误。
1) Something to do with my writeable stream. 1)与我的可写流有关。 When I try to create the file based on the
item.title
from the RSS feed, I get this error every time: 当我尝试基于RSS feed中的
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'
Does this have something to do with either the parentheses or the em-dash in the item title? 这与项目标题中的括号或破折号有关吗? If not, what else could be causing this issue?
如果没有,还有什么可能导致此问题?
2) When I do change the code (to name the writeable stream to something more simple) my code will throw the following error: 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)
This error is thrown usually after I've downloaded a number of the PDFs, but not all. 我下载了许多PDF(但不是全部)后,通常会引发此错误。 What can I change in this example to get past these errors?
为了克服这些错误,在此示例中我可以做些什么更改? Thank for your help!
谢谢您帮忙!
The problem is that the some of item.title
's contain /
character which indicates a folder that does not exist in this case. 问题是
item.title
的某些包含/
字符,表示在这种情况下不存在的文件夹。
It works when you get rid of those /
from title. 当您从标题中删除那些
/
时,它将起作用。 Eg 例如
const file = fs.createWriteStream(`${item.title.replace('/', '-')}.pdf`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.