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

Leave a Reply

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