JavaScript is used on the frontend and backend.
Frontend: Read files, no writing.
On the frontend, we can read files when the user uploads them, but we can’t write files programmatically — a user would have to initiate a download to write files from a website to their computer. This makes sense from a security stand point — imagine if a website could just start writing files on to your computer as soon as you visited it, without clicking on anything. Viruses and keyloggers would be every where.
This confused me at first since there are File APIs in JavaScript that are accessible in the frontend (File API — Web APIs | MDN (mozilla.org)). However, these are all for reading and manipulating uploaded files.
Backend: Read and write files.
Using NodeJS, we have utilities for writing files.
- fs
- fs-promise
Both are similar, but you will most likely need to use fs-promise
which returns a promise as its result, versus fs
which takes a callback.
In this article we will focus on fs-promise
.