Skip to content

Instantly share code, notes, and snippets.

@debonx
Created January 19, 2020 18:39
Show Gist options
  • Save debonx/9034807714731454d40fd8541f5b9160 to your computer and use it in GitHub Desktop.
Save debonx/9034807714731454d40fd8541f5b9160 to your computer and use it in GitHub Desktop.
Node: Writable stream, to get data from file and write on another.
const readline = require('readline');
const fs = require('fs');
// Create interface with a reading stream
const myInterface = readline.createInterface({
input: fs.createReadStream('shoppingList.txt')
});
// Create a write stream to custom file
const fileStream = fs.createWriteStream('shoppingResults.txt.');
// Design callback to output in writable file
const transformData = (line) => {
fileStream.write(`They were out of: ${line}\n`);
}
// Output on each line read
myInterface.on('line', transformData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment