const http = require('http');
const server = http.createServer((req, res) => {
// Request handler code
});

✅ What is req (IncomingMessage)?

Represents the incoming request from the client.

req.url → The path of the request (e.g., /home, /about).

req.method → The HTTP method used (e.g., GET, POST).

req.headers → Object containing request headers.

req.on('data', chunk) → Lets you read incoming data from the request body (e.g., for POST/PUT).

📌 Example:


console.log(req.url);    // "/home"
console.log(req.method); // "GET"
console.log(req.headers); // { host: ..., user-agent: ... }

✅ What is res (ServerResponse)?

Represents the server’s response you send back to the client.

res.writeHead(statusCode, headers) → Sets the HTTP status and headers.

res.write(data) → Writes chunks of the response body.

res.end(data) → Ends the response (required), optionally sending final data.

res.setHeader(name, value) → Sets a response header.

📌 Example:


res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Hello, world!\\n');
res.end('Goodbye!'); //for a string

🚨 Important:

✅ You must call res.end() to finish the response — otherwise, the client’s request will hang.

🚀 Summary: req → lets you read what the client sent.

res → lets you send your response back to the client.