January 2018
Intermediate to advanced
336 pages
7h 22m
English
Earlier in this chapter, we wrote a series of Node.js programs that could watch files for changes. Now let’s explore Node.js’s methods for reading and writing files. Along the way we’ll see two common error-handling patterns in Node.js: error events on EventEmitters and err callback arguments.
There are a few approaches to reading and writing files in Node. The simplest is to read in or write out the entire file at once. This technique works well for small files. Other approaches read and write by creating Streams or staging content in a Buffer. Here’s an example of the whole-file-at-once approach:
| | 'use strict'; |
| | const fs = require('fs'); |
| | fs.readFile('target.txt', (err, ... |
Read now
Unlock full access