Express Routing
๐ Covered in this chapter:
app.get/post/put/delete() ยท Route Parameters ยท Query Parameters ยท express.Router()
Define GET, POST, PUT, PATCH and DELETE routes in Express, work with route/query parameters, and use the Express Router.
1Express Routing โ What You'll Learn
Define GET, POST, PUT, PATCH and DELETE routes in Express, work with route/query parameters, and use the Express Router.
Here's everything this chapter covers, in the order you'll learn it:
- GET routes
- POST routes
- PUT routes
- PATCH routes
- DELETE routes
- Route parameters (:id)
- Query parameters (?search=)
- Route handler functions
- The Express Router for modular routes
- Nested routers
- API versioning
- 404 handling in Express
2Working Example
๐ป Example: Express Routing
JavaScript
โถ Run in Compiler
import express from "express";
const app = express();
app.use(express.json());
app.get("/api/courses/:id", (request, response) => {
const { id } = request.params;
response.json({ id, title: "Node.js Master Course" });
});
app.listen(3000);
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- request.params holds route parameters (like :id); request.query holds query string parameters (like ?search=node).
- express.Router() lets you group related routes into their own file and mount them with app.use(), keeping large apps organized.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about express routing?
Focus on: app.get/post/put/delete() ยท Route Parameters ยท Query Parameters ยท express.Router(). These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for express routing?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.