Learn nodejs, Standard input and standard output

Another feature of the process object is standard input and standard output. These two objects offer us a way to communicate with a process while it is running. For now, we will use these objects to read and write data to the Terminal.

Later on in the course, we’re going to use the standard input and standard output objects to communicate with a child process. Let’s go ahead and navigate to our Exercise Files. Under the Start folder, there’s an empty JavaScript file marked ask.js, let’s go ahead and open up that file. Now, we’ve already really been using the standard output object.

The console log uses the standard output object to actually log messages to the console. It logs the message and also controls the line spacing of a message to give you a new line. Let’s go ahead and write a message to the console just using standard output. process.stdout is our standard output object and we can use the write method to write some strings to the Terminal.

So I’ll go ahead and write Hello, and then I’ll go ahead and open this up again, and this time I’m going to go ahead and write World, but I’m going to go ahead and throw some line breaks in there, so we’ll throw some \ns, which will give us a new line.

Process standard output will write strings to the terminal, however it does not give you new lines automatically. Let’s go ahead and save this and then let’s navigate to our Terminal and run this file. So we can run this file simply by typing node ask. You will also notice that we have two process.stdout.write calls that are making this one Hello World string, so we have to actually control the line spacing when using standard output. Let’s go back to our code. What I’m going to do now is I’m just going to create an array of questions to ask.

questions = and we will set up a new array, and I’m just going to go ahead and ask three questions in this array. What is your name will be the first question, What is your fav hobby? And then we will also ask What is your preferred programming language? There we go. So we’re going to ask these three questions from an array and we’re also going to store the answers in an array.

So I’ll create another array down here for our answers. And that’ll be an empty array. Alright, now what I’m going to do is create a function that will allow us to ask a question, function ask, and what we’re going to do is take in the index of the question that you’d like to ask. If you would like to ask the first question of this array you’ll pass the ask function zero, if you would like to ask the last question in this array you would pass the ask function two. So what we’re going to do now is we are actually going to copy these stdout.write calls here and place them inside of the function.

So I’m going to go ahead and cut them from there and paste them inside of our function, give it a little bit of spacing, and instead of writing Hello World what we actually want to write is the question. So we want this to ask one of the questions from the array. I’m going to use another template string, so we’re using those “, and in this template string I’m going to go ahead and throw a couple of new lines there, and then I will go ahead and use the ${ questions, that’s the array, and I’m going to use the i that’s passed to this function to index the question that we should ask.

Now after we ask the question I’m also going to add a little prompt here, so instead of writing World we’ll just write a couple of spaces, a prompt, and then a couple more spaces. Great. So now we have a function for asking a question, let’s use it. Let me try to ask question 0. There we go. So I can go ahead and save this, and then navigate out to my Terminal, and from the Terminal I can go ahead and clear the screen by typing clear. If you’re on a PC you can do this in the command prompt with cls.

And now I’m going to run our app, so I’ll node ask. Now notice, using process standard output has screwed up the way our Terminal is working. So it does ask the question, and then our process exits, and now we can see that we are in the start folder and we see another prompt right here after the question. So as you can see our standard output object is just running everything together, it doesn’t even put out new lines when our program ends. Let’s go ahead and add a listener to our program to listen for the answers to these questions.

So I’m going to go ahead and go back to Sublime, and then from Sublime what I want to do is right under this function here I’m going to wire up an event listener for data on the standard input object. So process.stdin, standard input, on data. So when a data event is raised, this means that when the user types some data into the Terminal and hits enter we will raise this data event here. So I’m going to go ahead and add a callback function to handle this.

So when the user enters some data, or when any data is sent to our application through the standard input object this callback function will fire and that data that is sent to our application will come in as an argument. So let’s just go ahead and echo our data. What I’m going to do is say process.stdout.write, and we will go ahead and first write a new line, and then we will add that to data.toString. So we’ll take our data, we will convert it to a string, and we’ll also trim it.

And the reason that we want to trim this data string is it’s going to come with some leading and trailing \ns and slash \rs, so we want to get rid of those. And I will also just add my own \n there at the end, there we go. So when there’s any data sent to this application through the Terminal this callback function will fire and we will echo the data back by writing it to the Terminal and displaying it to our user. Let’s go ahead and save this and navigate out to the Terminal and see what’s happening. Now I can run my app ask. And as we can see we’re being asked for our name.

Notice that the application didn’t stop. The reason that this process isn’t over is because it’s still running. This is the first time we’re using node.js asynchronously. We are waiting for some input and when we add that input it will be handled with that asynchronous callback. So what is my name? Alex. Oh, we see that echoed. Hello world. Every time I hit enter I’m sending some data to the application, raising a data event on process standard input, and then we are writing that data back to you using standard output.

So when you have a running application it is going to continue to run until it encounters an error or you stop it. The way that we stop these applications is by hitting control C. So this is a running application here in the Terminal, hitting control C will kill this application and exit us back to the Terminal. I’ll go ahead and clear this. Let’s go back into our code and what we want to do from our code this time is actually save your answers and ask the next question, as apposed to just echoing your answer back to you. So from the process.stdin.on data listener that we have here I’m going to delete this line that echos the data that you’ve entered back to you, and instead of echoing the data, what I want to do is save it to the answers array.

answers.push will push an item into the array. I’m going to push data.toString .trim back into the array. So that’s your answer, you’ve filled out the answer and hit enter, we’re going to collect that and push it into the answers array. Now I want to check to see if there are any more questions. If answers.length is questions.length then you still have some questions left to ask.

So what we’re going to go ahead and do is just ask the next question. We will ask answers.length. And what this does is if you’ve answered one question the answers.length will be one. So we will now ask you the next question in the questions array, What is your favorite hobby? If you’ve answered two questions the answers.length will be two, which we can use to index the question in the questions array, What is your preferred programming language? So as so long as there are more questions to ask we will ask them.

Now if there aren’t any more questions to ask, meaning that the answers.length is greater than questions.length we will force this process to exit. process.exit will cause the process to exit from within. So when we hit line 21 we should be kicked back out to the Terminal prompt. I’m going to go ahead and save this and navigate to the Terminal and let’s run our application again. node ask will ask What is your name? Go ahead and type in Alex. What is your favorite hobby? Skiing.

What is your preferred programming language? JavaScript. And you can see that after I answer that third question we are kicked backed out to the Terminal prompt. That’s because the process.exit was invoked. We had answered all the questions, there weren’t anymore questions to ask, so we exit the process. What we want to do instead of exiting the process is display our answers back to our user. So we can add another listener here just before the first question is asked on line 28 and in this listener what we’re going to do is listen for an exit event on the process object.

process.on exit will listen for an exit event on the process object. And when the process.exit is invoked this callback function will fire. So when we exit the process we can do a couple of things just before the process exits. What I want to do is display the answers back to the user. So the first thing I’m going to do is give it a little bit of padding. process.stdout.write and I’m just going to write some padding in the form of new lines, so I’ll go ahead and add a couple new lines there.

There’s some padding at the top. I’m going to go ahead and copy this and paste it just to have padding at the top and the bottom. So between this process.stdou.write new line sandwich we want to actually write your answers to you. So now I’m going to process.stdout.write and this time because I want to display the answers I’m going to use another template string, so we’re going to use those “ characters and I’m going to say Go and I can find your hobby through answers 1, because it’s the second question that you ask, and I can find your name through answers 0, because it is the first question I ask, and then we’ll go ahead and find your preferred language through answers 2, or the last question that you asked, so just before the process exits we are going to echo back to you your question answers.

Let’s go ahead and save this and go back to the Terminal and again, we can clear the previous screen by typing clear, or cls if you are on a PC. And now I can node ask and here’s the first question, my name is Alex, the second question, skiing is my favorite hobby, and JavaScript is my favorite programming language. And we can see that we echo back to our user Go Skiing Alex you can finish writing JavaScript later. And then we are exited out of the process. So process.stdin and process.stdout are ways that we can communicate with a running process.

Leave a Reply

avatar
1000
  Subscribe  
Notify of