File System (fs) & HTTP Module Methods
Node.js provides a wide variety of built-in methods for working with files, networking, data, and more. Below are some of the key methods and functionalities categorized by their core modules:
1. File System (fs) Module Methods
The fs module allows you to work with the file system on your machine.
- fs.readFile(path, callback): Asynchronously reads the contents of a file. const fs = require('fs');fs.readFile('example.txt', 'utf8', (err, data) => {if (err) throw err;console.log(data);});
- fs.writeFile(file, data, callback): Asynchronously writes data to a file. fs.writeFile('example.txt', 'Hello, World!', (err) => {if (err) throw err;console.log('File has been saved!');});
- fs.appendFile(file, data, callback): Appends data to a file. fs.appendFile('example.txt', 'Data to append', (err) => {if (err) throw err;console.log('Data appended to file!');});
- fs.unlink(path, callback): Deletes a file. fs.unlink('example.txt', (err) => {if (err) throw err;console.log('File deleted');});
2. HTTP Module Methods
The http module is used to create HTTP servers and handle HTTP requests and responses.
- http.createServer(callback): Creates an HTTP server. const http = require('http');http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/plain' });res.end('Hello, World!');}).listen(3000);
- req.on(event, callback): Listens for an event on the request (like 'data', 'end'). req.on('data', chunk => {console.log(`Received chunk: ${chunk}`);});
- res.write(data): Writes a response body to the client. res.write('Hello, ');res.write('World!');res.end();
- res.end(): Signals that the response has been completed. res.end('Response finished.');
Path Module Methods
The path module provides utilities for working with file and directory paths.
- path.join([...paths]): Joins multiple path segments into one. const path = require('path');const joinedPath = path.join('/foo', 'bar', 'baz/asdf');console.log(joinedPath); // Outputs: '/foo/bar/baz/asdf'
 
 
 
