2. fs (File System)
The fs (File System) module in Node.js allows you to interact with the file system to read, write, and manipulate files and directories. Here are some of the most commonly used methods in the fs module
fs.readFile() & fs.readFileSync()
Reads the contents of a file asynchronously and synchronously.
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
const data = fs.readFileSync('example.txt', 'utf8');
console.log(data);
fs.writeFile() & fs.writeFileSync()
Writes data to a file asynchronously and synchronously.
fs.writeFile('example.txt', 'Hello, World!', (err) => {
if (err) throw err;
console.log('File written successfully');
});
fs.writeFileSync('example.txt', 'Hello, World!');
console.log('File written successfully');
fs.appendFile() & fs.appendFile()
Appends data to a file asynchronously and synchronously.
fs.appendFile('example.txt', 'Hello, World!', (err) => {
if (err) throw err;
console.log('File written successfully');
});
fs.appendFileSync('example.txt', 'Hello, World!');
console.log('File written successfully');
fs.rename() & fs.renameSync()
Renames or moves a file asynchronously and synchronously.
fs.rename('example.txt', 'renamed.txt', (err) => {
if (err) throw err;
console.log('File renamed successfully');
});
fs.renameSync('example.txt', 'renamed.txt');
console.log('File renamed successfully');
fs.unlink() & fs.unlinkSync()
Deletes a file asynchronously and synchronously.
fs.unlink('example.txt', (err) => {
if (err) throw err;
console.log('File deleted');
});
fs.unlinkSync('example.txt');
console.log('File deleted');
fs.mkdir() & fs.mkdirSync()
Creates a new directory asynchronously and synchronously.
fs.mkdir('newDirectory', (err) => {
if (err) throw err;
console.log('Directory created');
});
fs.mkdirSync('newDirectory');
console.log('Directory created');
fs.existsSync()
Checks if a file or directory exists synchronously.
const exists = fs.existsSync('example.txt');
console.log(exists ? 'File exists' : 'File does not exist');
fs.copyFile()
Copies a file asynchronously from one location to another.
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) throw err;
console.log('File copied successfully');
});
There are more methods you can use for that check
4. URL()
The url module in Node.js provides utilities for URL parsing, formatting, and resolving. This module is useful for handling and manipulating URL strings in web applications.
new URL()
Creates a new URL object, which parses a given URL and provides access to its components.
URL.searchParams
Creates a new URL object, which parses a given URL and provides access to its components.
const { URL } = require('url');
const myUrl = new URL('https://example.com/path?name=John&age=30');
console.log(myUrl.searchParams.get('name')); // Output: John
URL.toString() & URL.toJSON()
Converts a URL object into a string representation. Serializes the URL as a JSON string
const { URL } = require('url');
const myUrl = new URL('https://example.com/path?name=John');
console.log(myUrl.toString());
// Output: https://example.com/path?name=John
console.log(myUrl.toJSON());
// Output: "https://example.com/path?name=John"
URL.hostname & URL.port
Gets or sets the hostname portion of the URL (without port). Gets or sets the port portion of the URL.
const { URL } = require('url');
const myUrl = new URL('https://example.com:8000/path?name=John');
console.log(myUrl.hostname);
// Output: example.com
console.log(myUrl.port);
// Output: 8000
There are more methods you can use for that check
SOCIAL SHARE CARD GENERATOR