Title: Advanced Routing in Express.js
Routing in Express.js is one of the core functionalities that enables you to create dynamic web applications. Advanced routing techniques allow you to handle complex application logic, modularize your routes, and manage different HTTP methods effectively. This chapter covers advanced routing concepts including route parameters, query strings, handling different HTTP methods, modular route handlers, route-level middleware, and using the Express Router.
A. Route Parameters and Query Strings
Route parameters and query strings allow you to pass dynamic values in URLs, which can be used to customize the response.
-
Route Parameters:
-
Route parameters are dynamic parts of the URL, defined using a colon (
:) in the route path. -
They can be accessed using
req.paramsin the route handler. -
Example:
const express = require("express"); const app = express(); app.get("/users/:id", (req, res) => { const userId = req.params.id; res.send(`User ID: ${userId}`); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
In this example,:idis a route parameter that captures the user ID from the URL.
-
-
Query Strings:
-
Query strings are additional parameters sent in the URL after the
?symbol. -
They can be accessed using
req.query. -
Example:
const express = require("express"); const app = express(); app.get("/search", (req, res) => { const query = req.query.q; res.send(`Search results for: ${query}`); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
This example demonstrates how to handle query strings. The search term is passed as a query string (?q=term) and accessed viareq.query.q.
-
B. Handling Different HTTP Methods
Express.js allows you to handle different HTTP methods (GET, POST, PUT, DELETE, etc.) for the same route, enabling you to create RESTful APIs.
-
Example:
const express = require("express"); const app = express(); app.get("/resource", (req, res) => { res.send("GET request received"); }); app.post("/resource", (req, res) => { res.send("POST request received"); }); app.put("/resource", (req, res) => { res.send("PUT request received"); }); app.delete("/resource", (req, res) => { res.send("DELETE request received"); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
This example shows how to handle different HTTP methods for the/resourceroute. Each method has its own route handler.
C. Creating Modular Route Handlers
As your application grows, it's important to modularize your route handlers to keep your code organized and maintainable. Express.js allows you to separate routes into different files and import them into your main application.
-
Example:
-
Create a separate route file:
-
Create a file named
userRoutes.js:const express = require("express"); const router = express.Router(); router.get("/:id", (req, res) => { res.send(`User ID: ${req.params.id}`); }); module.exports = router;
-
-
Import the route file in your main application:
-
In your
server.jsorapp.js:const express = require("express"); const userRoutes = require("./userRoutes"); const app = express(); app.use("/users", userRoutes); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
This example demonstrates how to create a modular route handler in a separate file (userRoutes.js) and import it into your main application. The routes are prefixed with/users.
-
-
D. Route-Level Middleware
Route-level middleware functions are functions that are executed before the route handler. They are useful for tasks like authentication, validation, and logging.
-
Example:
const express = require("express"); const app = express(); const logRequest = (req, res, next) => { console.log(`Request received: ${req.method} ${req.url}`); next(); }; app.get("/dashboard", logRequest, (req, res) => { res.send("Welcome to the dashboard!"); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
In this example,logRequestis a route-level middleware that logs the request method and URL before passing control to the route handler.
E. Using Express Router
The express.Router class is used to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a "mini-app".
-
Example:
-
Create a Router:
-
In a file named
productRoutes.js:const express = require("express"); const router = express.Router(); router.get("/", (req, res) => { res.send("Product List"); }); router.get("/:id", (req, res) => { res.send(`Product ID: ${req.params.id}`); }); module.exports = router;
-
-
Mount the Router in your application:
-
In your
server.jsorapp.js:const express = require("express"); const productRoutes = require("./productRoutes"); const app = express(); app.use("/products", productRoutes); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });Explanation:
This example shows how to useexpress.Routerto create a separate router for product-related routes. The router is mounted at/products, making the routes accessible under that path.
-
-
Conclusion
Advanced routing in Express.js provides the flexibility to create dynamic and modular web applications. By understanding how to work with route parameters, query strings, different HTTP methods, modular route handlers, route-level middleware, and the Express Router, you can build complex, maintainable, and scalable applications.