Skip to content

Commit 6fcbf5b

Browse files
authored
docs: Added streams example to reading-files-with-nodejs (#7500)
* docs: Added streams example to reading-files-with-nodejs * docs: moved import to top and updated authors * spelling
1 parent 10c21c4 commit 6fcbf5b

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

apps/site/pages/en/learn/manipulating-files/reading-files-with-nodejs.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Reading files with Node.js
33
layout: learn
4-
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, clean99
4+
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, clean99, benhalverson
55
---
66

77
# Reading files with Node.js
@@ -88,3 +88,48 @@ All three of `fs.readFile()`, `fs.readFileSync()` and `fsPromises.readFile()` re
8888
This means that big files are going to have a major impact on your memory consumption and speed of execution of the program.
8989

9090
In this case, a better option is to read the file content using streams.
91+
92+
```mjs
93+
import fs from 'fs';
94+
import path from 'path';
95+
import { pipeline } from 'node:stream/promises';
96+
97+
const fileUrl = 'https://www.gutenberg.org/files/2701/2701-0.txt';
98+
const outputFilePath = path.join(process.cwd(), 'moby.md');
99+
100+
async function downloadFile(url, outoutPath) {
101+
const response = await fetch(url);
102+
103+
if (!response.ok || !response.body) {
104+
throw new Error(`Failed to fetch ${url}. Status: ${response.status}`);
105+
}
106+
107+
const fileStream = fs.createWriteStream(outoutPath);
108+
console.log(`Downloading file from ${url} to ${outoutPath}`);
109+
110+
await pipeline(response.body, fileStream);
111+
console.log('File downloaded successfully');
112+
}
113+
114+
async function readFile(filePath) {
115+
const readStream = createReadStream(filePath, { encoding: 'utf8' });
116+
117+
try {
118+
for await (const chunk of readStream) {
119+
console.log('--- File chunk start ---');
120+
console.log(chunk);
121+
console.log('--- File chunk end ---');
122+
}
123+
console.log('Finished reading the file.');
124+
} catch (error) {
125+
console.error(`Error reading file: ${error.message}`);
126+
}
127+
}
128+
129+
try {
130+
await downloadFile(fileUrl, outputFilePath);
131+
await readFile(outputFilePath);
132+
} catch (error) {
133+
console.error(`Error: ${error.message}`);
134+
}
135+
```

0 commit comments

Comments
 (0)