Node.js Authentication & JWT Quiz – 20 Questions with Answers
20 questions · 15 min · Medium
Test your knowledge of the Node.js File System (fs) module, including reading and writing files, creating and removing directories, file statistics, synchronous and asynchronous methods, and file system operations.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. To interact with the file system
The built-in fs module provides APIs for working with files and directories on the file system.
A. const fs = require('fs');
In CommonJS, the built-in fs module can be loaded using require('fs').
A. fs.readFile()
fs.readFile() asynchronously reads the contents of a file.
A. fs.readFileSync()
fs.readFileSync() reads a file synchronously and blocks execution until the operation completes.
A. fs.writeFile()
fs.writeFile() asynchronously writes data to a file, replacing the file if it already exists unless a different flag or method is used.
A. fs.appendFile()
fs.appendFile() adds the specified data to the end of a file, creating the file if it does not exist.
A. fs.mkdir()
fs.mkdir() is used to create a new directory.
A. fs.readdir()
fs.readdir() reads the contents of a directory and returns its entries.
A. fs.unlink()
fs.unlink() asynchronously removes a file from the file system.
A. fs.stat()
fs.stat() retrieves information about a file or directory, such as its size and timestamps.
A. isDirectory()
The isDirectory() method on an fs.Stats object returns true when the referenced path represents a directory.
A. utf8
Using the utf8 encoding with fs.readFile() causes the file contents to be returned as a string instead of a Buffer.
A. They can block the event loop while the operation completes
Synchronous file system operations block the JavaScript thread until they finish, which can reduce server responsiveness when used during request handling.
A. fs.rename()
fs.rename() can rename a file or directory and can also move it when the destination path is on the same file system.
A. It provides file system methods that return Promises
Node.js provides a promise-based file system API through fs/promises, allowing file operations to be used with async and await.