A.2. REPL

When you have Node.js installed, your first stop in running your code is in the Read-Evaluate-Print Loop (REPL). This interactive environment is similar to the console window in a Chrome web browser. In REPL, you’re able to run any JavaScript code. You can al so require Node.js modules to test aspects of your application.

A.2.1. Running JavaScript in REPL

To start REPL, navigate to any terminal window on your computer and enter node. This command immediately returns a prompt (>), after which you may enter any JavaScript statements. You can think of REPL as a running Node.js application that responds to your commands instantaneously. That is, you don’t need to write your JavaScript code in a separate file and then run it; you can type that JavaScript code directly in the REPL window. Try defining a couple of variables, as shown in the next listing. You’ll notice that with each JavaScript statement you run, REPL outputs the return value of that statement. For variable assignment, the return value is undefined.

Listing A.9. Defining variables in REPL
> let x = 42;
undefined
> let sentence = "The meaning of life is ";
undefined

Now perform some operation on these variables. You can concatenate the two values, for example, as shown in the following listing.

Listing A.10. Concatenating variables in REPL
> sentence + x;
The meaning of life is 42

There’s no limit to the ways you can use the REPL environment to behave like any Node.js application you’ve used or seen before. You can also use the tab key to autocomplete variable or function names and list object properties. If you defined a string by the variable name sentence, for example, but you’re unsure what functions you can call on that string, you can add a dot (.) to the end of the variable name and press Tab to list that variable’s available functions and properties, as shown in the next listing.

Listing A.11. Listing variable properties in REPL
> sentence.
sentence.anchor                sentence.big
sentence.blink                 sentence.bold
sentence.charAt                sentence.charCodeAt
sentence.codePointAt           sentence.concat
sentence.endsWith              sentence.fixed
sentence.fontcolor             sentence.fontsize
sentence.includes              sentence.indexOf
sentence.italics               sentence.lastIndexOf

You can find additional REPL commands in lesson 1.

A.2.2. Using REPL in application development

One other useful way to use REPL is through the repl module within your Node.js application code. As you build more custom modules in your project, you’ll notice that it’s tedious to load all those files into REPL to test the functionality of the code you’ve written. If you wrote a module called multiply.js (listing A.12) that contains a function to multiply two numbers, you’d need to require that module into REPL by entering require("./multiply") along with every other module you created. What’s more, you’d need to enter these lines for every new REPL session.

Listing A.12. Creating a single-function module in multiply.js
module.exports = {
  multiply: (x, y) => {
    return x * y;
  }
};

Instead of requiring your modules into each REPL session, you could bring REPL into your modules. Listing A.13 shows how you could use the repl module within your project. You can create a module within your project directory called customRepl.js that requires all the modules you want to test at the same time. This file shows the repl module being required and then a REPL server starting. Like a Node.js HTTP server, this REPL server has a context within which you can load custom variables. After the REPL server is started, add a name variable and your multiply module.

Listing A.13. Using the repl module in customRepl.js
const repl = require("repl"),
  replServer = repl.start({
    prompt: "> ",
});

replServer.context.name = "Jon Wexler";
replServer.context.multiply = require("./multiply").multiply;

All you need to do now is navigate to your project directory in terminal and enter node customRepl. You’ll see the REPL prompt, only this time, the context of your REPL session contains all the modules you want to test. This technique comes in handy when you want to test creating or modifying records in your database without having to copy and paste the code to require your database configurations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183