The core modules of Node.js are:

1. Path – The Path module provides utilities for working with file and directory paths. Example:

const path = require(‘path’);

const fileName = path.basename(__filename);

console.log(fileName); // Outputs: ‘myFile.js’

2. File System – The File System module provides a way of working with the file system on your computer. Example:

const fs = require(‘fs’);

fs.readFile(‘myFile.txt’, (err, data) => {
if (err) throw err;
console.log(data);
});

3. HTTP – The HTTP module provides a way of creating an HTTP server to listen for requests and respond to them. Example:

const http = require(‘http’);

const server = http.createServer((req, res) => {
res.writeHead(200, { ‘Content-Type’: ‘text/plain’ });
res.end(‘Hello Worldn’);
});

server.listen(3000, ‘127.0.0.1’);

4. Streams – The Streams module provides a way of handling streaming data. Example:

const fs = require(‘fs’);
const readStream = fs.createReadStream(‘./myFile.txt’, ‘utf8’);
const writeStream = fs.createWriteStream(‘./myFileCopy.txt’);

readStream.on(‘data’, (chunk) => {
writeStream.write(chunk);
});

Leave a Reply

Your email address will not be published. Required fields are marked *