DevAcademy
LearnNode.jsExpress Routing
IntermediateNode.js

Express Routing

Define routes for different URLs and HTTP methods, including dynamic route parameters.

Reading Time

12 min

Lesson

Lesson 19 of 34

Defining Routes

A route pairs an HTTP method and a URL pattern with a handler function. Express provides a method for each HTTP verb — app.get(), app.post(), app.put(), app.delete(), and so on.

Basic Routes

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Route Parameters

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Route Pattern Examples

PatternMatches
/api/users/:idAny single segment, captured as req.params.id
/api/users/:id/posts/:postIdTwo captured parameters
/files/*A wildcard matching any remaining path

Organizing Routes with express.Router()

As an app grows past a handful of routes, express.Router() lets you group related routes into their own module — like all /api/users routes — which is then "mounted" onto the main app at a specific path prefix.

Splitting Routes Into a Router

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Best Practice

Split routes into separate Router modules by resource (users, orders, products) once an app grows past a handful of routes — it keeps app.js focused on wiring things together rather than containing every route handler.

Interview Questions

Quick Quiz

1. How do you access a value from a dynamic route parameter like /users/:id?

2. What does express.Router() provide?

3. Which method registers a handler for HTTP DELETE requests?