Skip to content

Commit

Permalink
- script was not quitting, because the '\n' was not being accounted for
Browse files Browse the repository at this point in the history
- added text inspect, so that it is clear that the '\n' is present on the data
  • Loading branch information
marcooliveira committed Jun 12, 2012
1 parent b0cb29d commit 4e47475
Showing 1 changed file with 3 additions and 3 deletions.
@@ -1,4 +1,3 @@

So you've got a little CLI tool, but you want to be able to prompt a user for additional data after the script has started, rather than passing it in as a command line argument or putting it in a file. To do this, you'll need to listen to STDIN ("standard input", i.e. your keyboard), which Node.js exposes for you as `process.stdin`, a readable stream.

Streams are Node's way of dealing with evented I/O - they're a big topic, and you can read more about them (here). For now, we're only going to deal with the Stream methods relevant to working with `process.stdin` so as to keep the examples easy.
Expand All @@ -9,10 +8,11 @@ Here's a simple example. Try the following in a new file:

process.stdin.resume();
process.stdin.setEncoding('utf8');
var util = require('util');
process.stdin.on('data', function (text) {
console.log(text);
if (text === 'quit') {
console.log('received data:', util.inspect(text));
if (text === 'quit\n') {
done();
}
});
Expand Down

0 comments on commit 4e47475

Please sign in to comment.