Chapter 5: CLI and FS APIs: Your First Application

This chapter examines some of the most essential Node.JS APIs: those related to the handling of stdin and stdout of a process (stdio) and those related to the filesystem (fs).

As discussed in the preceding chapter, the Node approach to concurrency inherently involves the use of callbacks and events. These APIs provide your first contact with the flow control involved in nonblocking evented I/O programming.

In addition to learning how all these APIs interact together, you create your first application: a simple command-line file explorer, whose goal is to allow the user to read and create files.

Requirements

Start by defining what you want your program to do:

• You want the program to run on the command line. This means that the program is summoned either with the node command or by executing it directly, and then provides interaction with the user input and output through the terminal.

• Upon starting, the program should display the list of current directories (see Figure 5-1).

9781119963103-fg0501.eps

Figure 5-1: List of current directories displayed at startup.

• After you select a file, the program should output its contents.

• After you select a directory, the program should display its children.

• Then the program should quit.

Considering these cases, you can break down the project into different steps:

1. Creating our module

2. Deciding ...

Get Smashing Node.js: JavaScript Everywhere, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.