Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all text from stdin to a string

I'm writing a program in Node.js that (in some situations) wants to act as a simple filter: read everything from stdin (up to end of file), do some processing, write the result to stdout.

How do you do the 'read everything from stdin' part? The closest solutions I've found so far, seem to work either for one line at a time from the console, or else only work when stdin is a file not a pipe.

like image 561
rwallace Avatar asked May 25 '15 14:05

rwallace


1 Answers

My boiler-plate for this one is a lot like the solution described in a comment above -- offering it at the top level because it's very much the simplest way to do this and it shouldn't be only in a comment.

var fs = require('fs'); var data = fs.readFileSync(0, 'utf-8'); // Data now points to a buffer containing the file's contents 
like image 159
Patrick Narkinsky Avatar answered Sep 16 '22 21:09

Patrick Narkinsky