Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Node.js

You're reading from   Mastering Node.js Expert techniques for building fast servers and scalable, real-time network applications with minimal effort

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781782166320
Length 346 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Sandro Pasquali Sandro Pasquali
Author Profile Icon Sandro Pasquali
Sandro Pasquali
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Understanding the Node Environment 2. Understanding Asynchronous Event-Driven Programming FREE CHAPTER 3. Streaming Data Across Nodes and Clients 4. Using Node to Access the Filesystem 5. Managing Many Simultaneous Client Connections 6. Creating Real-time Applications 7. Utilizing Multiple Processes 8. Scaling Your Application 9. Testing your Application A. Organizing Your Work B. Introducing the Path Framework C. Creating your own C++ Add-ons Index

The Read-Eval-Print Loop and executing a Node program

Node's REPL (Read-Eval-Print-Loop) represents the Node shell. To enter the shell prompt, enter Node via your terminal without passing a filename:

> node

You now have access to a running Node process, and may pass JavaScript commands to this process. For example, after entering 2+2 the shell would send 4 to stdout. Node's REPL is an excellent place to try out, debug, test, or otherwise play with JavaScript code.

Because the REPL is a native object, programs can also use instances as a context in which to run JavaScript interactively. For example, here we create our own custom function sayHello, add it to the context of a REPL instance, and start the REPL, emulating a Node shell prompt:

require('repl').start("> ").context.sayHello = function() {
    return "Hello"
};

Entering sayHello() at the prompt will result in Hello being sent to stdout.

Let's take everything we've learned in this chapter and create an interactive REPL which allows us to execute JavaScript on a remote server.

Create two files, repl_client.js and repl_server.js, using the following code, and run each in its own terminal window, such that both terminal windows are visible to you.

/*	repl_client.js   */
var net = require('net');
var sock = net.connect(8080);
process.stdin.pipe(sock);
sock.pipe(process.stdout);

/*	repl_server.js  */
var repl = require('repl')
var net = require('net')
net.createServer(function(socket) {
  repl
  .start({
    prompt : '> ',
    input    : socket,
    output  : socket,
    terminal  : true
  })
  .on('exit', function () {
    socket.end()
  })
}).listen(8080)

repl_client simply creates a new socket connection to port 8080 through net.connect, and pipes any data coming from stdin (your terminal) through that socket. Similarly, any data arriving from the socket is piped to stdout (your terminal). It should be clear that we have created a way to take input and send it via a socket to port 8080, listening for any data that the socket may send back to us.

repl_server closes the loop. We first create a new TCP (Transmission Control Protocol) server with net.createServer, binding to port 8080 via .listen. The callback passed to net.createServer will receive a reference to the bound socket. Within the enclosure of that callback we instantiate a new REPL instance, giving it a nice prompt (> here, but could be any string), indicating that it should both listen for input from, and broadcast output to, the passed socket reference, indicating that the socket data should be treated as terminal data (which has special encoding).

We can now type something like console.log("hello") into the client terminal, and see hello displayed.

To confirm that the execution of our JavaScript commands is occurring in the server instance, type console.log(process.argv) into the client, and the server will display an object containing the current process path, which will be repl_server.js.

It should be clear from this demonstration that we have created a way to remotely control Node processes. It is a short step from here to multi-node analytics tools, remote memory management, automatic server administration, and so forth.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime