http.get请求返回csv.gz文件。 解压缩后,我想将其转换为JSON并上传到mongoDB 如何在流上实现转换功能?
我的代码如下:
const download = (url, dest) => {
const request = https.get(url, function(response) {
const decompress = zlib.createGunzip();
const file = fs.createWriteStream('./downloads/'+ dest);
response.pipe(decompress).on('data', (chunk) => {
console.log(chunk)
})
.pipe(file);
});
}
CSV至JSON将使用以下格式进行转换:
const csvToJson = (csv) => {
const [firstLine, ...lines] = csv.split('\n');
return lines.map((line) => firstLine.split(',').reduce(
(curr, next, index) => ({
...curr,
[next]: line.split(',')[index],
}),
{},
));
};
调用代码如下:
linksToDownload.map((item) => {
if (item.value !== undefined) {
const URL = item.value;
const DEST = item.name + ".json"
download(URL, DEST);
}
})
此外,为什么nodemon重新启动几次? printscreen of console
预先感谢您的帮助