- In the next couple of lessons,
we're going to build a simple question and answer app
that runs in the terminal.
So I'm going to start off by creating a new file
in our folder called questions.js.
Another feature of the process object
is standard input and standard output.
These two objects offer us a way to communicate
with this process while it's running.
For now, we're just going to use these objects
to read and write data from the terminal.
And, in this lesson, we're just going to be taking a look
at process.stdout.
So process dot standard output is a writeable string,
and it implements a write method.
So we can use this write method
to send data out of our program.
In this case, we can actually write a string
to the terminal.
So I'll do Hello space,
and then I'll do another call
to process.stdout.write World,
and we'll add a couple of new lines in there as well.
So I'm going to go ahead and open our terminal
and run this so we can take a look at the output.
Node questions.
And when we take a look at the output,
we see Hello World is printed with three extra new lines.
So what you might have noticed
is we've already been been using standard output,
however, console log adds a new line for each message,
so every log will appear on its own line.
The process standard output dot write method
gives us a little bit more control over the string
so you'll notice that even though I made two calls
to stdout.write, we are still only seeing
Hello World printed on one line.
So let's go ahead and use the standard output object
to ask some questions.
So the first thing I'm going to do is delete what we have here.
I'm also going to collapse the terminal.
And we're going to create some questions.
And these questions will be an array of strings.
What is your name?
What would you rather be doing?
And what is your preferred programming language?
We're also going to create a function
that we can use to ask question.
So we can also create an ask function
that we can use to ask questions.
Now, within this ask function, we'll take in an argument,
which will be the index of the question that we want to ask,
and if no argument is sent, that index will be zero.
So we can use process dot standard output dot write.
I'll use back tick so we can use a template string.
We'll add a couple of new lines to this template string.
And we will ask a question using the questions array.
And we will ask the question at the index.
So if i is zero, we're going to ask the first question,
what is your name?
If i is two, we're going to ask the last question
in this array, what is your preferred programming language?
We're also going to do another call to stdout.write
so that we can prompt the user for an answer.
And we'll just put a little prompt there.
Great.
So now let's go ahead and use this.
Let's ask a question.
So I'll go ahead and invoke the ask function,
and then we can go ahead and take a look at the terminal
and run our app.
So you'll notice that we have written to the terminal
three new lines, that's the white space that we see,
before the question what is your name
and our little prompt caret.
And then the program exits.
Notice that using standard output write
kind of screwed up the terminal.
We actually put the next terminal start prompt here,
after our standard output write.
So that's important to remember
that we're in control over new lines
if we want to use them
when we're using standard output write.
Now the reason that the program exits here
is because this node JS module,
along with every module we've built thus far,
is running synchronously.
What that means is, we declare the questions array,
we declare a function for asking the question,
and then once we ask the question,
node JS sees that there's nothing else to do,
so it stops running.
In the next lesson, we're actually going to finish this
by looking at process standard input
and see what it means to create
our first asynchronous node JS script.