WHAT’S IN THIS CHAPTER?
Node has a streaming API for dealing with files as if they were network streams. It is very convenient, but it only allows you to deal with files in a continuous way. This doesn’t work if you need to write in or read from specific positions inside a file. For that, you need to go down a level and deal with the filesystem itself.
This chapter covers file-handling basics, including how to open a file, read specific parts from it, write to it, and close it.
Much of Node’s file API is an almost direct translation of the UNIX (POSIX) APIs, such as the way that file descriptors are used. Just like in UNIX, a file descriptor handle in Node is an integer representing the index of an entry on the process file descriptor table.
There are three special file descriptors — 1, 2, and 3. They represent, respectively, the standard input, standard output, and standard error file descriptors. Standard input is, as the name indicates, a read-only stream that the process can use to read from the console or from data piped from another process. Standard output and standard error are output-only file descriptors, and they can be used to output to the console or into another process or file. Standard error