Working with files and directories is a very common task. In this recipe, we will see how we can do this using CoffeeScript and Node.
In this recipe, we have a file named chinook.sqlite
and a directory named
src
. Both are part of the source code from Chapter 9, Testing
Our Applications, but you can use any file and directory renamed to match this setup.
Node provides the filesystem module to work with files and directories. It allows us to create, open, close, read, write, and rename files and directories. To see how we can perform these tasks, follow these steps:
Using Node's filesystem module, we can copy a file by creating read and write streams and piping file contents from one to the other:
fs = require 'fs' src = 'chinook.sqlite' dest = 'chinook2.sqlite' # copy from a read stream into a write stream fs.createReadStream(src).pipe fs.createWriteStream(dest)