Handling requests and responses
Handling requests and responses in Node.js typically involves using the built-in http
module or a more feature-rich framework like Express.js. I'll explain both approaches.
1. Using the http
Module
The http
module is a core module in Node.js that allows you to create a server to handle HTTP requests and send responses.
Example:
const http = require('http');
// Create an HTTP server
const server = http.createServer((req, res) => {
// Handle incoming requests and send responses
// Log the request method and URL
console.log(`Received ${req.method} request for: ${req.url}`);
// Set the content type to text/html
res.setHeader('Content-Type', 'text/html');
// Send a response back to the client
res.statusCode = 200; // Set status code
res.end('<h1>Hello, World!</h1>'); // Send a response body
});
// Start the server on port 3000
server.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Explanation:
http.createServer()
: Creates a new HTTP server.req
: Represents the incoming request, containing details like the URL, HTTP method, headers, etc.res
: Represents the outgoing response, allowing you to set headers, status codes, and send data back to the client.res.end()
: Ends the response, and sends the data to the client.
2. Using Express.js
Express.js is a popular web framework for Node.js that simplifies handling requests and responses.
Installation:
First, you need to install Express.js using npm:
npm install express
Example:
const express = require('express');
const app = express();
// Handle GET request to the root URL
app.get('/', (req, res) => {
// Log the request method and URL
console.log(`Received ${req.method} request for: ${req.url}`);
// Send a response back to the client
res.status(200).send('<h1>Hello, World with Express!</h1>');
});
// Start the server on port 3000
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Explanation:
app.get()
: Handles GET requests to the specified path ('/'
in this case).req
: The request object, similar to the one in thehttp
module.res
: The response object, with convenient methods likeres.send()
to send data to the client.app.listen()
: Starts the server and listens on the specified port.
Key Differences:
http
module: More manual control, but requires more code to handle common tasks like routing, parsing request bodies, etc.- Express.js: Provides a higher-level abstraction, making it easier to build web applications with less boilerplate code.
Which method do you prefer, or would you like more details on a specific aspect?
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.