Oct 2, 2020
Feeding Data in NodeJS
I used to work the library readline
when I wanted to feed data into a nodejs command line application. Now I found a slightly niftier solution by using a library called split
.
Code of the Day
This is how you do it:
// we stream the input and send the messages ones by one.
const split = require('split');
const input = process.stdin.pipe(split());
input.on('data', (line) => {
if(line) {
console.log(line)
}
});
notice the if(line)
, this is because when feeding data I usually get an empty line at the end.