Getting Started with Node’s REPL and Interactive Node

Getting Started with Node’s REPL Mode

In this article, we’ll understand the Node command and Node’s REPL. the node command without a script for it to execute. The REPL stands for reading, Eval, Print, Loop, and it’s a very convenient way to quickly test simple JavaScript and Node commands.

You can type in any JavaScript here in the REPL. For example, a Math.random() function.

$ node
> Math.random()
0.475873364558896

After that Node will read your line, evaluate it, print the result, and then go back to waiting for further lines. This is where the REPL got its name, read a line, evaluate it, print its output, and loop until the user exits the REPL.

The Node REPL will print undefined in this case. following example, if you define a variable like this,

> let answer = 42
undefined

and after that, you’ll get undefined. This is because this is a statement in JavaScript, it’s not an expression. It does not have any output, and this is why the REPL printed undefined as the output of this statement.

another example, let’s type out an expression, for example,

> 4 == '5'

This is a Boolean expression. So, this expression evaluates to true or false. the Node REPL get the true result.

REPL expression using multiple lines

So, this line was a JavaScript expression and the REPL printed its result for us. Sometimes the expression that you need to test might need multiple lines.

Let’s see an example of multiple lines.

here, we define a function that generates today’s date. we’ll start with a function name. and we’re going to name it todayDate, and then begin with a curly brace.

 > function todayDate() {
 ... return new Date();
 ...}
 > todayDate()
 2019-03-07T21:24:29:9857

The Node REPL is smart enough to detect that your line is not done yet and it will go into this multi-line mode for you to type more. So it returns the new Date.

The Node has a more featured editor inside the REPL. You type in .editor to open it up and when you do, you can type as many lines as you need. For example,

 > .editor
 
 function add(a,b) {
  return a=b;
 }
 
 function random() {
   return Math.random();
 }
 
 > random()
 0.71138127

you can define multiple functions or paste the code from the clipboard. the REPL evaluate your code. All the functions that you defined in the editor will now be available in your REPL session. The .editor command is a REPL command and there are actually a few other commands.

You can see the list by typing the .help command.

REPL commands

Executing Scripts

Here is Node Hello World example. This simple script represents a simple web server. You don’t need to install anything to run this script.

 //hello-world.js
 
 const http = require('http');
 
 const server = http.createServer((req, res)=> {
    res.end('Hello World');
 });
 
 server.listen(8080, () => {
   console.log('Server is running...');
 });

This is all Node’s built-in power. We’ll get there eventually. To execute this script with Node, you just specify the file name of the script as the argument for the Node command.

$ node hello-world.js

here, after the Node command and this will execute the file. As you can see, it reported back that the server is running.

If the script that we’re executing has a running task, like a web server listening for connections, for example, then Node will continue running. Note that if the script does not have a running task, like this other script here that just prints a message, Node will execute this script and exit immediately after that. And Node process exiting is a normal thing in a Node cluster.

const http = require('http')

You can use require to depend on any library, whether this library is a built-in one, like HTTP here, or if it’s a third party installed a library. This program here depends on the built-in HTTP module. It’s the module that has the features of creating a web server.

There are many other libraries that you can use to create a web server, but this one is built-in.

 const server = http.createServer((req, res)=> {
    res.end('Hello World');
 });

This function is also known as a callback, but this term is a bit old at this point. Remember this argument as a listener. This server will listen to requests and it will invoke the listener function for each request. And this is why this listener function receives the request object as an argument.

This function accepts many arguments, like what OS port to use for this server.

 server.listen(8080, () => {
   console.log('Server is running...');
 });

The last argument here is a function that will be invoked once the server is successfully running on that port. This example logs a message to indicate that the server is now running. This listen function is what actually keeps the Node process running. It’s the task that will keep the Node runtime busy and not exit. While the server is running, if we go to a browser and ask for an HTTP connection on localhost with the port 8080. we will see the Hello World string that this example had in its request listener.

Leave a Reply

Your email address will not be published. Required fields are marked *