Routing determines how an application responds to a client request for a specific endpoint, a combination of a URL path and an HTTP method. This lesson covers how Express matches and dispatches requests to the right handler.
What Is Routing in Express?
A route in Express is defined by calling a method named after an HTTP verb, app.get(), app.post(), app.put(), app.delete(), and passing it a path and one or more handler functions.
When a request arrives, Express walks through registered routes in the order they were defined and dispatches to the first one whose method and path both match.
METHOD is any HTTP verb in lowercase: get, post, put, patch, delete.
PATH can be a plain string, a string with named parameters (:id), or a regular expression.
HANDLER receives (req, res, next) and is responsible for sending a response.
app.all() matches a path regardless of HTTP method.
Express Routing Cheatsheet
The most common routing patterns you will write in nearly every Express app.
Pattern
Example
Matches
Exact path
app.get('/about', fn)
GET /about only
Named parameter
app.get('/users/:id', fn)
GET /users/42, req.params.id === '42'
Multiple params
app.get('/users/:userId/posts/:postId', fn)
Nested resource paths
Optional segment
app.get('/products/:id?', fn)
/products and /products/5
Any method
app.all('/status', fn)
GET, POST, PUT, DELETE, etc.
Chained methods
app.route('/users').get(a).post(b)
Same path, multiple methods
Route Matching Order Matters
Express checks routes top to bottom and stops at the first match unless that handler calls next(). This means a generic route defined earlier can accidentally "shadow" a more specific one defined later.
// BAD: this catches everything, /new never gets a chance to run
app.get('/users/:id', getUserById);
app.get('/users/new', showNewUserForm);
// GOOD: specific literal paths before parameterized ones
app.get('/users/new', showNewUserForm);
app.get('/users/:id', getUserById);
Always place more specific, literal paths before parameterized wildcard-style paths.
Organizing Routes as Your App Grows
Defining every route directly on app works for small examples, but real applications quickly move routes into express.Router() instances (covered in the Express Router lesson) grouped by resource, users.routes.js, orders.routes.js, and mount them under a common prefix.
Group related routes by resource, not by HTTP method.
Mount grouped routers under a prefix: app.use('/api/users', userRouter).
Keep route files focused on wiring, move logic into controllers/services.
Common Mistakes
Placing a parameterized route (/users/:id) before a literal route (/users/new), silently breaking the literal one.
Defining the same method and path twice, only the first registered handler ever runs.
Using app.get() for a route that actually creates or mutates data, breaking REST conventions.
Forgetting the leading slash in a path string.
Key Takeaways
Routes pair an HTTP method with a path and one or more handler functions.
Express matches routes in registration order and stops at the first match.
Literal paths should be registered before similar parameterized paths.
As an app grows, group routes into express.Router() modules instead of one giant file.
Pro Tip
When two routes could match the same URL, run app.get('/users/new', ...) mentally against app.get('/users/:id', ...) and ask which one Express would hit first, order bugs like this are extremely common.
You now understand how Express matches routes. Next, look closely at every HTTP method Express supports in the Route Methods lesson.