O'Reilly logo

JavaScript Concurrency by Adam Boduch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Evented file IO

Now that we have a fairly good handle on network IO in NodeJS, it's time to focus our attention on file system IO. After this section, we'll see how files and network sockets are treated the same inside the event loop. Node takes care of the subtle differences for us, which means we can write consistent code.

First, we'll look at reading form files, followed by writing to files. We'll close the section with a look at streaming from one file to another, performing data transformations in between.

Reading from files

Let's start with a simple example that reads the entire contents of a file into memory. This will help us get a feel for doing asynchronous file IO:

// We need the "fs" module to read files. var fs = require('fs'); var path ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required