Introduction
Routing refers to how an application responds to a client request for a specific endpoint — a combination of a URL path and an HTTP method (GET, POST, PUT, DELETE, etc.). In Express, routes are defined using methods on the app object that correspond to HTTP verbs. Each route associates a path pattern with one or more handler functions that execute when the route is matched.
Cricket analogy: Routing is like a broadcaster deciding which camera feed to show for a specific request, combining what's being asked (boundary replay) with how (live vs highlights); each route pairs a URL path with a handler the way each camera angle pairs a call sign with an operator.
Syntax
app.METHOD(PATH, HANDLER);
// Examples
app.get('/users', (req, res) => { /* ... */ });
app.post('/users', (req, res) => { /* ... */ });
app.put('/users/:id', (req, res) => { /* ... */ });
app.delete('/users/:id', (req, res) => { /* ... */ });Explanation
METHOD is an HTTP method in lowercase such as get, post, put, patch, or delete. PATH is a string, string pattern, or regular expression that describes the URL, and can include named route parameters prefixed with a colon (e.g. :id). HANDLER is a callback function invoked with (req, res, next) when the route matches. Express also provides app.all() to match any HTTP method, and express.Router() to create modular, mountable route handlers.
Cricket analogy: METHOD like get or post is the type of action (viewing vs submitting a review), PATH like /matches/:id names which fixture, and HANDLER(req, res, next) is the umpire's ruling function; app.all() is like a rule that applies regardless of the specific action, and express.Router() is like a modular sub-panel of umpires for one ground.
Example
const express = require('express');
const app = express();
app.get('/products/:id', (req, res) => {
const { id } = req.params;
res.json({ productId: id, name: 'Sample Product' });
});
app.get('/search', (req, res) => {
const { q } = req.query;
res.json({ query: q, results: [] });
});
app.listen(3000);Output
A GET request to /products/42 returns {"productId":"42","name":"Sample Product"}. A GET request to /search?q=laptop returns {"query":"laptop","results":[]}. Express extracts :id into req.params.id and query string values into req.query automatically.
Cricket analogy: A GET to /matches/42 returns {"matchId":"42","venue":"Sample Ground"} like a scorecard pulled up by match number; a GET to /search?q=finals returns {"query":"finals","results":[]} with Express automatically extracting :id into req.params.id and the search term into req.query.
Key Takeaways
- Routes combine an HTTP method and a path pattern with a handler function.
- Route parameters (e.g. :id) are accessed via req.params; query strings via req.query.
- express.Router() lets you group related routes into modular, mountable route handlers.
- app.all() matches a path for all HTTP methods, useful for logging or authentication checks.
Practice what you learned
1. What does app.get('/users/:id', handler) capture in req.params?
2. Which Express feature lets you group routes into a separate, mountable module?
3. How do you access the value of a query string parameter like ?q=laptop?
4. Which method registers a route handler that matches any HTTP method for a given path?
Was this page helpful?
You May Also Like
Introduction to Express
Learn what Express is, why it exists on top of Node's http module, and how to create your first Express server.
Route Parameters and Query Strings
Understand how to capture dynamic URL segments and optional query data in Express routes.
RESTful API Design
Learn the core principles of REST: resource-based URLs, proper HTTP methods, and meaningful status codes.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics