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
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 pagehttp://localhost:3000/about→About us
2. A route's format
app.METHOD(PATH, HANDLER)| Element | What it is | Example |
|---|---|---|
METHOD | The HTTP method (lowercase) | get, post, put, delete |
PATH | The path the route responds to | '/', '/users', '/users/:id' |
HANDLER | The (req, res) handler function | (req, res) => res.send('OK') |
3. Examples of different routes
GET, retrieve data
app.get('/users', (req, res) => {
res.send('List of users');
});POST, create
app.post('/users', (req, res) => {
res.send('User created');
});PUT, update
app.put('/users/:id', (req, res) => {
res.send(`User ${req.params.id} updated`);
});DELETE, remove
app.delete('/users/:id', (req, res) => {
res.send(`User ${req.params.id} deleted`);
});4. Dynamic routes
Routes can contain parameters, marked with :name.
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():
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:
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:
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:
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 readyA concise answer to help you respond confidently on this topic during an interview.