~3 min read • Updated Dec 26, 2025
1. Introduction to Routing in Express
Routing determines how an application responds to HTTP requests. Each route consists of:
- A route path
- An HTTP method (GET, POST, PUT, DELETE, etc.)
Express uses app.get(), app.post(), app.all(), and app.use() to define routes and middleware.
2. Route Methods
Route methods correspond to HTTP methods. Example:
// GET route
app.get('/', (req, res) => {
res.send('GET request to homepage')
})
// POST route
app.post('/', (req, res) => {
res.send('POST request to homepage')
})
app.all() handles all HTTP methods:
app.all('/secret', (req, res, next) => {
console.log('Accessing secret section...')
next()
})
3. Route Paths
Route paths can be defined using strings, patterns, or regular expressions.
3.1 String-Based Paths
app.get('/about', (req, res) => res.send('about'))
app.get('/random.text', (req, res) => res.send('random.text'))
3.2 String Pattern Paths
(Note: These patterns behave differently in Express 5.)
app.get('/ab?cd', (req, res) => res.send('ab?cd'))
app.get('/ab+cd', (req, res) => res.send('ab+cd'))
app.get('/ab*cd', (req, res) => res.send('ab*cd'))
app.get('/ab(cd)?e', (req, res) => res.send('ab(cd)?e'))
3.3 Regular Expression Paths
app.get(/a/, (req, res) => res.send('/a/'))
app.get(/.*fly$/, (req, res) => res.send('/.*fly$/'))
4. Route Parameters
Route parameters capture dynamic values from the URL and store them in req.params.
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
Example:
URL: /users/34/books/8989
req.params = { userId: "34", bookId: "8989" }
Parameters can include hyphens or dots:
/flights/:from-:to /plantae/:genus.:species
Restricting parameters with regex:
app.get('/user/:id(\\d+)', ...)
5. Route Handlers
Express allows multiple callback functions for a single route.
5.1 Single Handler
app.get('/example/a', (req, res) => res.send('Hello A'))
5.2 Multiple Handlers
app.get('/example/b', (req, res, next) => {
console.log('Next handler...')
next()
}, (req, res) => res.send('Hello B'))
5.3 Array of Handlers
app.get('/example/c', [cb0, cb1, cb2])
5.4 Combination of Arrays and Functions
app.get('/example/d', [cb0, cb1], (req, res, next) => {
next()
}, (req, res) => res.send('Hello D'))
6. Response Methods
Key res methods that end the request-response cycle:
| Method | Description |
|---|---|
| res.send() | Send a response |
| res.json() | Send JSON |
| res.redirect() | Redirect |
| res.render() | Render a template |
| res.sendFile() | Send a file |
| res.download() | Download a file |
| res.sendStatus() | Send status code |
7. app.route(): Chained Route Definitions
Use app.route() to define multiple handlers for the same path without repeating the path.
app.route('/book')
.get((req, res) => res.send('Get a book'))
.post((req, res) => res.send('Add a book'))
.put((req, res) => res.send('Update a book'))
8. express.Router: Modular Route Handlers
express.Router creates modular, mountable route handlers—essentially a “mini-app”.
Example birds.js
const router = express.Router()
router.use((req, res, next) => {
console.log('Time:', Date.now())
next()
})
router.get('/', (req, res) => res.send('Birds home'))
router.get('/about', (req, res) => res.send('About birds'))
module.exports = router
Mounting the Router
app.use('/birds', require('./birds'))
To access parent route parameters:
const router = express.Router({ mergeParams: true })
Conclusion
Express routing is one of the most powerful and flexible routing systems in the Node.js ecosystem. By combining simple paths, pattern-based routes, dynamic parameters, middleware-style handlers, app.route(), and express.Router, you can build modular, readable, and maintainable applications.
Written & researched by Dr. Shahin Siami