The fs module provides APIs for reading, writing, updating, deleting, and watching files.
For server request handling, asynchronous APIs are usually preferable because they avoid blocking the event loop while waiting for disk operations.
Deep dive
Common file operations
- readFile: read a file asynchronously.
- writeFile: create or replace a file.
- appendFile: add content.
- mkdir: create directories.
- unlink: delete a file.
Important safety rule
Never allow arbitrary user-provided file paths to directly control what the server reads or deletes. Restrict file access to approved directories and validate paths.