What is the http module in Node.js?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Node.js development. It reveals whether you understand the building blocks that more complex concepts rely on.

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.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Node.js project, I used this when...' immediately makes your answer more credible and memorable.