Digging Into Node.js

Outputting a Stream to a File

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 "Outputting a Stream to a File" 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 makes changes to the program such that if printing to standard output isn't specified, the output will be a text file, enabling a choice between the two streams. The help message is modified to reflect this change.

Preview
Close

Transcript from the "Outputting a Stream to a File" Lesson

[00:00:00]
>> Kyle Simpson: Let's update our program and then we'll update our help. We're gonna do something a little bit different now. By default, we've been printing to the standard out, but let's just for our purposes have a default of writing to a file, and then we'll have a configuration where you can dump to the standard out if you want.

[00:00:24]
But we'll assume that maybe you wanna write to another file. So i'm gonna create a variable called OUTFILE, and we want, actually let me do it afterwards, we want for OUTFILE to,
>> Kyle Simpson: Be the path.join with BASE_PATH, wherever that is.
>> Kyle Simpson: Concatenated with a file name of out.txt.

[00:00:57]
So we're basically just saying wherever it found the file, whatever kind of processing it is gonna write it back out to another file.
>> Kyle Simpson: Now, if we set up a boolean flag for out, this is going to allow us to tell it no, no, no, don't write it to that file, just jumped it to the standard out.

[00:01:20]
So we're gonna be able to control the target stream that we wanna go after.
>> Kyle Simpson: So down here in our process file when we are selecting the process.standard out what we wanna do is have some selection logic. If orgs.out, meaning we've told the program we want you to go to process.stdout, then we want to say targetStream= process.stdout.

[00:01:54]
But if it's not specified, then we wanna make a file system stream to dump the contents from the stream into another file. And remember, because it's stream processing, because of the piping, it's gonna dump only 65K each time, so it doesn't have to read the whole thing and then write the whole thing.

[00:02:15]
So how do we make an output stream? It's pretty straightforward, we're going to say targetStream is equal to fs.createWriteStream, and we're going to give it that half that we set up at the top.
>> Kyle Simpson: So now we're deciding which stream we want to pipe our contents to based upon our parameters that we pass in the command line.

[00:02:45]
>> Kyle Simpson: And by default, we wanna go to out.text
>> Kyle Simpson: Okay, so let's update our help just to make sure we don't forget that.
>> Kyle Simpson: --out says print to the stdout, we won't do that by default, but you can configure that on. So, if we go back to our code, if we run this without adding the --out, now we shouldn't see any output at all.

[00:03:28]
But if we look in the files directory, oops, I'm sorry, in this directory. You'll notice now we have an out.txt, which is our 1 megabyte copied file. And if we look at the contents of out.txt, you'll notice that it was the transformed copy of it.

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