What is the http module in Node.js?

Answer

The http module is a core Node.js module for creating HTTP servers and making HTTP requests. Creating a server: const server = http.createServer((req, res) => { res.writeHead(200, {"Content-Type": "text/plain"}); res.end("Hello World"); }); server.listen(3000);. The callback receives a IncomingMessage (request) object with properties like req.method, req.url, req.headers, and a ServerResponse object for sending responses. Making requests: http.get(url, callback) for simple GET requests; http.request(options, callback) for full control. The https module provides the same API with TLS/SSL support. In practice, most developers use frameworks like Express.js on top of the http module, as raw http is verbose for routing and middleware. For HTTP requests from Node.js, libraries like axios or the native fetch (Node.js 18+) are preferred.