Digging Into Node.js

Processing Input from stdin

Kyle Simpson

Kyle Simpson

You Don't Know JS
Digging Into Node.js

Check out a free preview of the full Digging Into Node.js course

The "Processing Input from stdin" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle modifies the program so the contents to be processed can be optionally pulled from the standard input stream, and introduces the util and get-stdin packages.


Transcript from the "Processing Input from stdin" Lesson

>> Kyle Simpson: We talked earlier about the standard in stream. We said we want to be able to pull from a standard in but we don't want to have to do our own like invention of that logic, so we're going to go ahead and pull in another package that I've already pre-installed for you called getStdin.

>> Kyle Simpson: get-stdin. While I'm up here at the top, I'm gonna go ahead and add another package. We're gonna use this one a little bit later, but I'm gonna add another package called util. That's another one of the built-in ones in node. So we want to be able to support not only telling you a file by name, but also we want our program to be able to receive the file inputs on the standard in stream.

So we need some way of indicating that to our program on a command line parameter. We need some flag to tell it hey get your stuff from standard in instead of trying to look for it in the file. So what we're going to do then is say, we're gonna add a boolean flag and we'll call this "in", and then we will handle "in" by saying if args.in we're gonna be getting from the

>> Kyle Simpson: Standard in, otherwise we're gonna be getting from a file system. Now you'll notice that we're going to wanna call process file, but we're gonna need to not pass it, necessarily a string path anymore. We're gonna want to go ahead and pass it, the contents of the file.

Cuz we either might have gotten it from a file or we might have gotten it from standard in. So I'm gonna change process file, basically I'm gonna take this code and move it, or at least some of it, I'm gonna move it up to here temporarily.
>> Kyle Simpson: So we're going to do "processfile(contents);"

>> Kyle Simpson: And we're gonna make processFile receive the contents. Now, what's it gonna do? It's gonna say contents = contents.toUpperCase.
>> Kyle Simpson: And then we're gonna call process.stdout.write(contents).
>> Kyle Simpson: This code should still work exactly the same, we just moved where that logic was happening. But we want to be able to support receiving that stuff from stdin.

We're gonna call the getStdin function and this is a promise returning mechanism. So that's our first sense of using asynchrony without a callback. This is a promise returning mechanism. And so it's going to resolve a promise with all the contents that it got from the standard in. It'll capture all of them and resolve to all of those.

So we're gonna call getStdin and then we're gonna say .then. And what we wanna do is now we know we will have those contents at this point so we can just call processFile. And if there's an error somewhere, we wanna catch it, and the catch should send the error message to our error handler.

>> Kyle Simpson: Okay, let's try that out. There's one more thing we wanna do. One of the conventions that is somewhat common within parameters speak is that if you have a hyphen, a single hyphen at the end of the line that's another way of saying, hey guess what? Standard in is gonna provide all the rest of the inputs.

So we wanna handle that and we know that minimist doesn't know how to process a single hyphen. So we're going to say, if args.in or,
>> Kyle Simpson: And remember we had the args._ was an array of all the things that it couldn't process. We're gonna say if that array includes the single hyphen.

Then that will tell us that we want to do standard in processing. Let's update our help.
>> Kyle Simpson: Let's try it again, we'll try it with something where it was already working, which was hello.txt.
>> Kyle Simpson: All right, cuz we need to make sure it's a string.
>> Kyle Simpson: So process file is now expecting already a string.

So we updated that to call toString on it.
>> Kyle Simpson: Now it's able to process it if we grab it from a file, but we also want to be able to do it with standard in input. So let's try that out. Let's say that we're going to use the built in command called cat.

We're gonna say files/hello.text and we're going to pipe that information into our ./ex1.js and we're just going to tell it to expect the process from standard in.
>> Kyle Simpson: And now it received it through standard in instead. Same thing if we do a single hyphen at the end. Yes?

>> Speaker 2: Could you maybe go a little more in-depth on what standard in is, I got lost there.
>> Kyle Simpson: Yeah, so if standard out is our output stream, where we're pushing from our program back out to the shell. Standard in is the stream of any contents that are currently in the buffer of our shell that are going to go into the next program.

So when we do a pipe command here, what we're saying is anything that this created as a standard output, it would normally just be dumped to the shell. But if we pipe it that's saying turn the output into an input stream, and then we're basically saying take that input stream and pipe it directly into this program.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now