- [Instructor] In the last lesson, we built
the first part of the question answer app,
which defines the questions as long
as they function to ask or prompt the user for each answer.
So in this lesson, we're going to use
the process.standardinput object to listen for answers.
So I'm going to continue working in the same file.
The first thing I'm going to do down here
is wire up a listener using process standard input,
and we're going to use the on function to listen for events.
The event name that we're listening for is a data event.
That means that you've typed something
into the keyboard and you've pressed enter.
In the second argument of the on function
is the handler or the function that
we will use to handle that data.
So I can collect your data as the
first argument that's sent back
to this callback function and do something with it.
For right now, we'll just echo it back to
the user using process standout write,
and we'll write a couple new lines,
and then we'll take the data,
which is currently passed to this
function as a buffer or a binary,
so we have to call .tostring on it,
and then I'm also going to trim it
so we get rid of any leading and
trimming spaces or commands.
And I'll go ahead and write a couple other new lines.
So what we've done here is we've
used the process as standard input object
to listen for data events.
We've also created our first asynchronous application.
So because this application is listening for input data,
the application will be open and running,
and every time we type in some data,
we'll see it in the terminal.
Look at this, I'm going to open up the terminal,
I'll move the code here so we can
look at our recent code, and node questions.
So here we can see what's your name, and I can type in Alex.
And notice the program still hasn't exited.
So because we've wired up this data event,
the program's going to continue running,
and continue listening for data.
So every time I type anything,
then we see it echo it back to the terminal.
So for this type of process, I'm going to
have to stop it by issuing control c.
And I'm going to come back up here to
my code and I'm just going to add one
line after we collect your answer,
and this line will be process.exit.
So using the process, I'm going to
invoke an exit event, so let's go
ahead and see what happens,
come back down here to the terminal, clear it out,
node question, what's my name?
Alex, it's echoed back, and because we
invoked a process exit on line 16,
the program exits and we're back
to the start prompt of the terminal.
Great, so now that we understand
how the process.standardinput object works,
let's complete our q and a app.
So instead of taking your data,
and just echoing it back to you,
what I'm going to do is copy this data
to string trim call, and I'm just going to
add this to an array called answers.
So first, let's create an array called answers,
and then I'm going to go ahead and say answers.push,
and we'll go ahead and push that data to string.trimcall.
So now I'm going to check the length of the answers array.
So if answers.length is less than questions.length,
what we want to do is ask the next question.
And we can send the index for the next
question from answers.length, so that
means that if I've answered one question already,
then we will ask the second question in the array.
Because answers.length will be one,
and that index will point to the second question.
Otherwise, we'll have all of the answers,
so I'm just going to invoke a process.exit.
Let's take a look at what's going to happen here.
On line 12 we asked the first question,
and when we ask the first question,
we don't send any index, so I is equal to zero,
which means we ask the first question in the array,
what is your name?
Now, as soon as I answer that question
by typing Alex and pressing enter,
this callback function will fire,
and we will take that answer, Alex,
and push it into the answers array.
Now that we have one answer in the array,
one is actually less than three,
or the length of the questions array,
so we're going to use that answers.length,
the value of one to ask the next question.
Inside of the s function, I becomes one,
and we ask the next question.
So we will iterate through all of the questions
using this little if else block right here.
The last thing I want to do is handle the process exit.
So whenever the process does exit,
I can write up another listener for an exit event.
And that listener can be handled
with a callback function passed as the second argument.
So what I'm going to do in here is
write your answers back to you.
Now remember, answers is an array,
so the first question was what is your name,
the second question was what would
you rather be doing and the third
question is what is your preferred programming language?
So the first answer is going to be your name,
then the second answer is going to be your activity,
what you'd rather be doing.
And the third answer is going to
be your preferred programming language.
So using array de-structuring, I can
set some local variables for activity and language.
We haven't talked about this much this far,
but when we used the template string,
we actually honor any white space.
So if I added new lines inside of this console log,
they would show up.
I'll print thank you for your answers.
So we'll use them like a mad lib,
and I will say go activity, name,
you can write lang, code later.
So now when the process exits,
this handler will fire because
we added a process on exit listener there on line 25.
So let's go ahead and run our application.
I'm going to run out to the terminal
and make it a little bigger, and clear it,
and we can run node questions,
and the first question is what is your name?
Alex, what would you rather be doing?
Skiing, what is your preferred programming language?
Node js, and when we hit enter
on that last question the process exit fires,
so we print this message that
says thank you for your answers,
and go skiing Alex, you can write node js code later.
Finally notice that the template string
that we're using honors light space.