Chapter 2

File System を手なづける

Wrangling the File System

プログラマーであれば、ファイルシステムにアクセスした経験があるはずです。ファイルシステムへのアクセスというのはつまり、ファイルを読み込んだり、ファイルに書き込んだり、ファイルの名称を変更したり、ファイルを削除する、という行為のことです。Node.js の長旅を、この慣れ親しんだ領域から始めることとしましょう。ファイルシステムに関連する諸作業を便利にしてくれるようなツールを作っていきます。しかもこれは「非同期」に処理をするツールです。

As a programmer, chances are you’ve had to access a filesystem at some point: reading, writing, renaming, and deleting files. We’ll start our Node.js journey in this familiar area, creating useful, asynchronous file utilities. Along the way we’ll explore the following aspects of Node.js development:

Node.js Core

On the architecture front, you’ll see how the event loop shapes a program’s flow. We’ll use Buffers for transporting data between Node.js’s JavaScript engine and its native core, and we’ll use Node.js’s module system to bring in core libraries.

ss

Patterns Inside our programs, we’ll use common Node.js patterns like callbacks for handling asynchronous events. We’ll harness Node.js’s EventEmitter and Stream classes to pipe data around. JavaScriptisms We’ll take a look at some JavaScript features and best practices such as block scoping and arrow-function expressions. Supporting Code You’ll learn how to spawn and interact with child processes, capture their output, and detect state changes. We’ll begin by creating a tool that watches a file for changes. This’ll give you a peek into how the event loop works while introducing Node.js’s filesystem APIs.

Wilson, Jim. Node.js 8 the Right Way: Practical, Server-Side JavaScript That Scales (Kindle Locations 395-405). Pragmatic Bookshelf. Kindle Edition.