Remember how we talked about the fs module for Node in the introduction? We saw that it was capable of reading html and plain text–however fs is capable of so much more.

What can we do with fs?

There are a multitude of things node’s file system module can do. Tt’s main uses include:

We’ve already somewhat covered how to read files within our previous http module demo! Now why don’t we look into how to create files using fs.

Creating Files

Now that we know of the primary uses for Node’s file system (fs), let’s get started with something simple; making a file!

Let’s start off by importing our module into our app,js file. If you remember from last time, we do that with the const fs = require(fs); command.

This includes the fs module in our node app and allows us to work with the files!

Now that we’ve done this, the rest is pretty straightforward.

We start off by first calling a method from the fs module called fs.writeFile(). The brackets here take up 3 different parameters;

  1. The path to the file, usually the file name or url – if there is no matching file it creates a new file under the specified name and content
  2. The content that the user wants written to the file that comes in the form of a string, Buffer, TypedArray or DataView (we will only be covering the form of strings)