Oluwatise Okuwobi
fs
Modulefs
module allows you to carry out several operations that involve files and folders. With the fs
module you can:fs.readFile()
method. For this to work you'll need to create the file you want to read. Let's assume that it's a txt file with the content hello world
. Let's call it dummyText.txt.http
module, which will read your file on the localhost, or using promises. I'm going to show you both, and which one is preferable.http
module. Open your JS file, let's call it readfile.js
, and add the following code:cmd
in the file directory and run Node readfile.js:C:\Users\nodefilesystem>node readfile.js
fs/Promises
to processes our files with promises.readFile
method to process our file that we want to read. Using promises we can read our files in an asynchronous manner. Inside the readFile
method we have the path to our dummytext
that we want to read, then return the content of the file.WriteFile
. It's pretty much the same code, but with a few adjustments:writeFile
method which creates the file directly and adds the data to it. Also take note of the await
keyword, which we need to run a successful JavaScript promise.dummyText.txt
to changedDummyText.txt
.appendFile()
, which basically changes key information, and allows you to write new contents of the file. We also talked about writeFile()
, which updates the contents of a file as well.