November 2013
Intermediate to advanced
148 pages
3h 12m
English
Earlier in this chapter, we wrote a series of Node programs that could watch files for changes. Now let’s explore Node’s methods for reading and writing files. Along the way we’ll see two common error-handling patterns in Node: error events on EventEmitters and err callback arguments.
There are a few different approaches to reading and writing files in Node. The simplest way 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:
| file-system/read-simple.js | |
| | const fs = require('fs'); |
| | fs.readFile('target.txt', function (err, ... |
Read now
Unlock full access