Chapter 6. File system: Synchronous and asynchronous approaches to files

 

This chapter covers

  • Understanding the fs module and its components
  • Working with configuration files and file descriptors
  • Using file-locking techniques
  • Recursive file operations
  • Writing a file database
  • Watching files and directories

As we’ve noted in previous chapters, Node’s core modules typically stick to a low-level API. This allows for various (even competing) ideas and implementations of higher-level concepts like web frameworks, file parsers, and command-line tools to exist as third-party modules. The fs (or file system) module is no different.

The fs module allows the developer to interact with the file system by providing

  • POSIX file I/O primitives
  • File streaming
  • Bulk file I/O
  • File watching

The fs module is unique compared with other I/O modules (like net and http) in that it has both asynchronous and synchronous APIs. That means that it provides a mechanism to perform blocking I/O. The reason the file system also has a synchronous API is largely because of the internal workings of Node itself, namely, the module system and the synchronous behavior of require.

The goal of this chapter is to show you a number of techniques, of varying complexity, to use when working with the file system module. We’ll look at

6.1. An overview of the fs module

6.2. Summary

sitemap