What are the core modules of Node.js?

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);
});

What are the core modules of Node.js?

The core modules of Node.js are:

1. HTTP – This module provides a way of creating an HTTP server and making HTTP requests. 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);

2. FS – This module provides a way of interacting with the file system. Example:

const fs = require(‘fs’);

fs.readFile(‘/path/to/file’, ‘utf8’, (err, data) => {
if (err) throw err;
console.log(data);
});

3. Path – This module provides utilities for working with file and directory paths. Example:

const path = require(‘path’);

const fileName = path.basename(‘/path/to/file.txt’);
console.log(fileName); // Outputs: file.txt

4. Stream – This module provides a way of streaming data. Example:

const fs = require(‘fs’);
const stream = require(‘stream’);

const readStream = fs.createReadStream(‘/path/to/file.txt’);

const writeStream = new stream.Writable();
writeStream._write = (chunk, encoding, done) => {
console.log(chunk.toString());
done();
};

readStream.pipe(writeStream);