Skip to main content

What is a route in Express?

1. What a route is

A route in Express.js is a mapping between:

  • an HTTP method (GET, POST, PUT, DELETE, …);
  • a URL path (/, /users, /api/products/:id);
  • and a handler function, that runs when a request matches it.

In other words, a route tells Express: "If a request for GET /users comes in, run this code."

The simplest route example

javascript
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Home page'); }); app.get('/about', (req, res) => { res.send('About us'); }); app.listen(3000, () => console.log('Server running'));

If you open these in a browser:

  • http://localhost:3000/Home page
  • http://localhost:3000/aboutAbout us

2. A route's format

javascript
app.METHOD(PATH, HANDLER)
ElementWhat it isExample
METHODThe HTTP method (lowercase)get, post, put, delete
PATHThe path the route responds to'/', '/users', '/users/:id'
HANDLERThe (req, res) handler function(req, res) => res.send('OK')

3. Examples of different routes

GET, retrieve data

javascript
app.get('/users', (req, res) => { res.send('List of users'); });

POST, create

javascript
app.post('/users', (req, res) => { res.send('User created'); });

PUT, update

javascript
app.put('/users/:id', (req, res) => { res.send(`User ${req.params.id} updated`); });

DELETE, remove

javascript
app.delete('/users/:id', (req, res) => { res.send(`User ${req.params.id} deleted`); });

4. Dynamic routes

Routes can contain parameters, marked with :name.

javascript
app.get('/products/:id', (req, res) => { res.send(`Product with ID: ${req.params.id}`); });

GET /products/123 → response: Product with ID: 123

Parameters can be read via req.params.

5. Grouping routes (Router)

To keep code organized, it's convenient to move routes into separate files using express.Router():

javascript
const express = require('express'); const router = express.Router(); router.get('/', (req, res) => res.send('List of products')); router.get('/:id', (req, res) => res.send(`Product ${req.params.id}`)); module.exports = router;

In the main file:

javascript
const productsRouter = require('./routes/products'); app.use('/products', productsRouter);

Now GET /products and GET /products/123 are handled by this module.

6. Routes can have several handlers

You can pass several functions (middleware) that run one after another:

javascript
app.get('/admin', (req, res, next) => { console.log('Checking authorization...'); next(); }, (req, res) => { res.send('Admin panel'); } );

7. Support for patterns and RegExp in routes

Express supports pattern routes and regular expressions:

javascript
app.get('/ab*cd', (req, res) => { res.send('Matched the pattern /ab*cd'); });

This matches /abcd, /ab123cd, /abXYZcd, and so on.

8. Summary

A route in Express.js is a rule that tells the server how to respond to a specific HTTP request.

It consists of:

  • an HTTP method (GET, POST, PUT, DELETE, etc.),
  • a path (/, /users/:id),
  • a handler function ((req, res)).

Routes:

  • drive the application's logic;
  • support dynamic parameters;
  • can be grouped into modules via Router;
  • support middleware and pattern paths.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.