Skip to content

Commit 2f40715

Browse files
eljefedelrodeodeljefeMyles Borins
authored and
Myles Borins
committed
doc: adds usage of readline line-by-line parsing
In order to make developers aware of node-core built-in functionality, which might replace module APIs, we should add an example of readline`s interface usage. SEO will eventually aid this goal, since it is well searched on Q&A sites. PR-URL: #4609 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]>>
1 parent 5b45a46 commit 2f40715

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

doc/api/readline.markdown

+16
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ line interface:
232232
process.exit(0);
233233
});
234234

235+
## Example: Read File Stream Line-by-Line
236+
237+
A common case for `readline`'s `input` option is to pass a filesystem readable
238+
stream to it. This is how one could craft line-by-line parsing of a file:
239+
240+
const readline = require('readline');
241+
const fs = require('fs');
242+
243+
const rl = readline.createInterface({
244+
input: fs.createReadStream('sample.txt')
245+
});
246+
247+
rl.on('line', function (line) {
248+
console.log('Line from file:', line);
249+
});
250+
235251
## readline.clearLine(stream, dir)
236252

237253
Clears current line of given TTY stream in a specified direction.

0 commit comments

Comments
 (0)