📍Standard Node Modules:

📍Standard Node Modules:

🔹File System (fs)

The file system module is one of the more often used modules available. It provides an API for working with the local file system.

to use this we make use of require( ) :

//example 
const fs = require("fs");

Functions in File System :

fs.copyFile( ):

fs. copyFile ( ) : is used to copy the contents of one file to another file. This is a asynchronous function.

// syntax:
fs.copyFile(sourceFile, destinationFile, callback)
// sourceFile : is string representing the path  to the file to be copied.
// destinationFile : is string representing the path to the file to be created.
// callback : is optional function that will be called when the copy operation is complete. It takes one paramenter , which is an error object.

Example:

const fs = require('fs');

fs.copyFile('source.txt','destination.txt',(err)=>{
if(err) throw err;
console.log('File copied successfully');
});

In this code snippet, we are copying the file named 'source.txt' to a new file named 'destination.txt', if this operation is successful, the console will display a message stating that the file is copied successfully. if an error occurs during copy operation error will be thrown.

Note: if the destination file i.e. destination.txt already exists then the content of destination.txt will be overwritten by the content of the source.txt.

video solution:

fs.copyFileSync( ):

fs.copyFileSync( ) : this method allows you to copy a file synchronously.

// syntax:
fs.copyFileSync(srcPath,destPath[, flags])
/*
srcPath : (string) , the path to the source file.
destPaht:  (string) , the path to destination file.
flags : (number), an optional integer that specifies the behaviour of the copy operation. flags can take vales like 'fs.constatns.COPYFILE_EXCL'  and 'fs.constants.COPYFILE_FICLONE'.
if you use flag value as fs.constants.COPYFILE_EXCL  , the destination file will not be overwritten if it already exists.
const fs = require('fs');

try {
  fs.copyFileSync('path/to/source/file.txt', 'path/to/destination/file.txt');
  console.log('File copied successfully!');
} catch (err) {
  console.error(err);
}

first, we import the 'fs' module.

the fs module has a method called copyFileSync, the method takes two arguments :

source and the other is the destination path, if the destination path file already exists then an error is thrown.

The file copied successfully is logged on to the console if the file is copied from source to destination.

if an error occurs during the copying process, the error is handled by try-catch block and the error is logged onto the console using the console.error().

video :

// adding 3 rd attribute / parameter in the

🔹HTTP and HTTPS( http and https)

HTTP vs HTTPS

HTTP : Hypertext transfer protocol, runs on port 80.

Https: Hypertext transfer protocol secure, runs on port 443.

Example program to create HTTP and HTTPS server uisng node.js:

const http = require('http');
const https = require('https');
const fs = require('fs');

// https:here we are reading ssl certificate and key file for HTTPS server.
// these files are used to establish a secure connection between client and server
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert')
};

const httpPort = 8080;
const httpsPort = 8443;

const handleReaquest = (req,res)=>{
res.writeHead(200);
res.end('Hello friends, How is the blog ? ');

};

const httpServer = http.createServer(handleRequest);
const httpsServer = https.createServer(options, handleRequest);

httpServer.listen(httpPort, ()=>{
console.log(`HTTP server listening on port ${httpPort}`);
});

httpsServer.listen(httpsPort,()=>{
console.log(`HTTPS server listening on port ${httpsPort}`);
});

🔹OS (os)

The os module provides a set of operating system-level utility functions that allow your code to be aware of the environment it's running in and make any necessary allowances for it.

os.EOL :

It provides end-of-line characters the operating system uses.

os.cpus():

It returns an object array, where each object gives you info about the CPU(s) in the system along with information like model, speed etc.

os.freemem():

It returns an integer value i.e. the free system memory in bytes.

os.homedir():

It returns a path to the current home directory.

os.hostname():

It returns the systems hostname.

🔹Path (path)

following are some of the functions that the Path module contains:

path.basename():

It returns the last portions of a path.

path.dirname():

It returns the path portion.

path.extname():

It returns the extension.

path.parse():

It returns an object in the form {root: ' ', dir: ' ', base: ' ', ext: ' ', name:' '}

path.join():

This method is used to join path segments into a final form and note it uses a platform-specific separator automatically

ex:

path.normalize():

This is used to deal with munged strings.

🔹Process

process.abort():

It aborts the Node process, ending your program.

process.exit():

process.version:

This contains the version of the node itself.

🔹Query String (querystring)

🔹URL (url):

url.pathToFileURL():

url.fileURLToPath():

🔹Utilities (util)

util.format():

Did you find this article valuable?

Support MadCoding7588's blog by becoming a sponsor. Any amount is appreciated!