Express.js works by handling client requests through a series of steps called the request-response cycle, using middleware and routing to process requests and send responses efficiently.
A client sends a request (e.g., browser or app) to the Express server.
The request flows through middleware that handles logging, authentication, and other processing tasks.
Express matches the request to a route handler based on the URL and HTTP method.
The route handler processes the request and prepares a response.
The server sends the response back to the client.
If any error occurs, it is caught by error-handling middleware to ensure the server remains stable.
Basic Routing in Express.js
Basic routing in Express.js is the process of defining URL endpoints (routes) and specifying how the server should respond to client requests at those endpoints.
Express provides simple methods to define routes that correspond to HTTP methods:
app.get() - Handle GET requests.
app.post() - Handle POST requests.
app.put() - Handle PUT requests.
app.delete() - Handle DELETE requests.
app.all() - Handle all HTTP methods.
The server handles different HTTP requests using routes (GET, POST, PUT, DELETE, ALL).
Each route sends a response when its specific URL is accessed.
The program does not block and can handle multiple requests efficiently.
The server starts and listens on port 3000.
Parameters in Express.js
Parameters in Express.js are values that are passed in the URL or request that your server can use to process the request dynamically. They allow your routes to handle different inputs without creating multiple static routes.
Types of Parameters in Express:
1. Route Parameters: Part of the URL (e.g., /users/:id) accessed via req.params.
2. Query Parameters: Appended to the URL (e.g., /search?term=nodejs) accessed via req.query.
3. Request Body Parameters: Sent in POST or PUT requests, accessed via req.body with express.json() middleware.
Middleware in Express.js
Middleware in Express.js is a function that executes during the request-response cycle. It has access to the request (req) and response (res) objects, and can either end the request-response process or pass control to the next middleware using next().
Middleware is used for
Logging requests
Authentication and authorization
Parsing request bodies
Handling errors
Built-in Middleware
Express includes several useful middleware functions:
Middleware logs each request method and URL before handling it.
next() passes control to the next middleware or route.
JSON middleware parses incoming request data.
The route / sends a response to the client.
The server runs on port 3000.
Error Handling in Express.js
Error handling in Express is done through special middleware functions that have four arguments:
(err, req, res, next).
The /error route creates and passes an error using next(err).
Error-handling middleware catches and handles the error.
The error is logged using console.error().
A response with error message and status is sent to the client.
The server runs on port 3000.
Usage of Express.js
Express.js is a popular web framework for Node.js that simplifies server-side development. It is widely used to build web applications, APIs, and scalable server-side solutions.
Web Applications: Build dynamic sites and SPAs efficiently.
RESTful APIs: Create APIs for CRUD operations and data delivery.
Middleware Apps: Add logging, authentication, and validation.
Microservices: Lightweight and modular for scalable services.