Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Constructing a custom REPL

From: Node.js Essential Training

Video: Constructing a custom REPL

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.

Constructing a custom REPL

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.

Show transcript

This video is part of

Image for Node.js Essential Training
Node.js Essential Training

52 video lessons · 12407 viewers

Joseph LeBlanc
Author

 
Expand all | Collapse all
  1. 3m 9s
    1. Welcome
      42s
    2. What you should know before watching this course
      57s
    3. Using the exercise files
      47s
    4. Using the challenges
      43s
  2. 18m 50s
    1. Why use Node.js?
      2m 1s
    2. Choosing a Node.js installation process
      4m 56s
    3. Installing Node.js on the Mac
      2m 5s
    4. Installing Node.js on Windows
      1m 43s
    5. Installing Node.js via Node Version Manager on Ubuntu Linux
      4m 45s
    6. Installing Node.js via Node Version Manager on a Mac
      3m 20s
  3. 13m 54s
    1. Exploring language additions to the V8 JavaScript engine
      3m 38s
    2. Understanding require() and modules
      6m 39s
    3. Understanding callback execution in the event loop
      3m 37s
  4. 12m 23s
    1. Initializing Node.js projects
      2m 37s
    2. Finding an NPM
      3m 30s
    3. Maintaining projects using the npm command
      6m 16s
  5. 21m 4s
    1. Creating modules with getters and setters
      6m 33s
    2. Understanding module caching and scopes
      6m 15s
    3. Implementing JavaScript creational patterns
      5m 8s
    4. Challenge: Leveraging module caching
      55s
    5. Solution: Leveraging module caching
      2m 13s
  6. 32m 27s
    1. Starting Express applications
      3m 42s
    2. Handling GET requests returning JSON
      3m 26s
    3. Navigating large datasets using request variables in routes
      5m 4s
    4. Modifying data through PUT requests
      4m 32s
    5. Supplying middleware to modify HTTP response headers
      4m 37s
    6. Generating HTML views in Jade
      8m 9s
    7. Challenge: List all records as JSON
      37s
    8. Solution: List all records as JSON
      2m 20s
  7. 25m 37s
    1. Evaluating unit testing methodologies
      3m 8s
    2. Isolating existing code for testing
      6m 47s
    3. Supplying helpers for test data
      2m 35s
    4. Writing unit tests with Mocha
      6m 5s
    5. Simulating HTTP requests with SuperTest
      7m 2s
  8. 28m 45s
    1. Connecting to MongoDB via Mongoose
      4m 16s
    2. Defining schemas for collections in Mongoose
      3m 47s
    3. Querying MongoDB collections
      5m 32s
    4. Persisting Express sessions through MongoDB and mongo-connect
      7m 6s
    5. Logging in to Express applications using Passport
      8m 4s
  9. 21m 47s
    1. Controlling the built-in REPL
      4m 18s
    2. Constructing a custom REPL
      5m 40s
    3. Receiving command-line arguments with Optimist
      6m 14s
    4. Building command-line tools
      3m 35s
    5. Challenge: Launch a custom REPL from a command
      41s
    6. Solution: Launch a custom REPL from a command
      1m 19s
  10. 15m 46s
    1. Emitting events and attaching listeners
      5m 32s
    2. Streaming chunked data through readable streams
      2m 33s
    3. Controlling readable streams
      1m 52s
    4. Piping readable data into writable streams
      2m 10s
    5. Handling duplex streams over TCP
      3m 39s
  11. 4m 6s
    1. Deploying projects to cloud hosting services
      2m 36s
    2. Finding Node.js resources
      1m 30s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Node.js Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.