Junior — Middle
What is the main function of the HTTP module in programming?
sobes.tech AI
Answer from AI
The main function of the HTTP module in Node.js is to create an HTTP server and client for handling HTTP requests and responses. This module allows accepting incoming requests from clients (e.g., browsers), processing them, and sending responses, enabling the creation of web servers and APIs.
Example of creating a simple HTTP server:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, world!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});