Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The standard node repl is good for exploring node in general, however it's also possible to create a custom repl interacting with an existing application. In this video we'll add a repl to an existing application. But first, let's just build one stand-alone. Go to the command line and change directory to the desktop, capital d. And I am going to touch the file repl.js. Now that we have that file, I'm going to open it in sublime text.
And I'm just going to set up a very basic repl that's almost exactly the same as what you get when you type node on the command line. So first I'm going to type var and repl and I'm going to set that to require repl. So this is a core node module. It's available out of the box and you don't have to install anything. And now I'm going to call repl.start and then I'm going to pass in an object. The one property I'm going to set on this object is called prompt. And you can set this to whatever you'd like.
Whatever you want to appear on every line where somebody enters in a command. And now finally let's start this up in terminal. Type Node space repl and then press Return. So now we have a prompt. And I can press Return, and it'll say undefined, I can do a equals 10, and then do b equals 20, and then I can add them together and it will show me the result. While this is interesting, it's not really serving much of a purpose beyond proving that we can create a custom repl.
However, we have an existing server with a data set used throughout the application. It would be helpful if we could inspect this data in real time and make modifications without going through the HTTP interface. To handle this we can use the repl module to build a custom prompt that works with the data while it's also being used for the server. So I'm going to hit Ctrl + C to get out of this and I'm going to hit that again and now I'm just going to close out this file. And now let's go to the exercise files and set up the dependencies so go to chapter eight video two and then copy the Start folder to the desktop.
Now we're going to change directory to that folder. So type CD space, and then I'm just going to drag that folder into terminal to get the path. And now I'm going to type MTM install. So that just installed all the modules I need for this application. So now let's open up the Start folder in Sublime Text and start making some modifications. So we can use a repl with the server. Go to server.js and this is where we're going to be able to add some pieces that will start that repl. So first, let's include that repl module again.
Next, I'm going to create that prompt just like I did in the other file, except this time, I'm going to assign it to a variable, and assigning it to a variable is going to let us work with it after we create it. So I'm going to type var prompt and then I'm going to start the repl here. And again I'm going to set the prompt property of this object. This time I'm going to set the prompt to flights. Now one thing to note about this repl is that it's currently living in its own context. Its context is just a straight up node environment with no variables attached.
That's not going to be useful if we want to use the data that we're using with this server though. So to fix that, we're going to assign the data that we're using to the context of this repl. To do that type in prompt and then press Dot and Context. So, the context is an object, where we can set any properties that we want to make available as global variables in this repl. So in this case, we want to set the data variable. And we're just going to set that data variable to the data that we have here So now I am going to save this file and let's start up the server.
Go to terminal and type node space server and then press return. So you will notice that it gave us a flights prompt and it also told us that the express server is listening on port 3000. It kind of jam both of these together because they both have it at once. So let's just press Return, and now we have a clean prompt here for flights. So now, let's go to the browser for a moment and I'm going to load up Flight 567. Go to local host at port 3000, and then go to flight/567. So you'll notice at the moment there's no actual arrive or actual departure time yet.
Now lets trigger the arrival on the command line and then watch the results in the browser. You'll notice again that the console logged out onto that prompt so let's press Return and now we get a nice clean prompt again. So first let's type data. When I type data it evaluated the Data Objects that we assigned to the context of this repl. I can also do data and then 567 in brackets, and that's just going to show the 567 flight. What I want to do is call the triggerArrive method on this flight.
So I'm going to press the up arrow on my keyboard to bring up Data 567 again. Then I'm going to press dot, and then I'm going to call triggerArrive. And after I press return its going to say undefined because that function doesn't return a value, but if I go up twice on the keyboard again and inspect that data element at 567 its going to show an actual arrive value here, now if I go back to the browser and hit Refresh you will see the same value here as well By creating your own REPL, you can gain control of an application as it runs.
Just be sure to set any variables you need in the REPL's context. In the next video we'll look at ways of handline command line arguements passed to your node programs.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96658 Viewers
56 Video lessons · 110372 Viewers
71 Video lessons · 79199 Viewers
131 Video lessons · 37982 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.