100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Node.js

Routing in Express

Understand how Express matches HTTP methods and URL paths to handler functions using routes.

Express FundamentalsBeginner9 min readJul 8, 2026
Analogies

Introduction

Routing refers to how an application responds to a client request for a specific endpoint — a combination of a URL path and an HTTP method (GET, POST, PUT, DELETE, etc.). In Express, routes are defined using methods on the app object that correspond to HTTP verbs. Each route associates a path pattern with one or more handler functions that execute when the route is matched.

🏏

Cricket analogy: Routing is like a broadcaster deciding which camera feed to show for a specific request, combining what's being asked (boundary replay) with how (live vs highlights); each route pairs a URL path with a handler the way each camera angle pairs a call sign with an operator.

Syntax

javascript
app.METHOD(PATH, HANDLER);

// Examples
app.get('/users', (req, res) => { /* ... */ });
app.post('/users', (req, res) => { /* ... */ });
app.put('/users/:id', (req, res) => { /* ... */ });
app.delete('/users/:id', (req, res) => { /* ... */ });

Explanation

METHOD is an HTTP method in lowercase such as get, post, put, patch, or delete. PATH is a string, string pattern, or regular expression that describes the URL, and can include named route parameters prefixed with a colon (e.g. :id). HANDLER is a callback function invoked with (req, res, next) when the route matches. Express also provides app.all() to match any HTTP method, and express.Router() to create modular, mountable route handlers.

🏏

Cricket analogy: METHOD like get or post is the type of action (viewing vs submitting a review), PATH like /matches/:id names which fixture, and HANDLER(req, res, next) is the umpire's ruling function; app.all() is like a rule that applies regardless of the specific action, and express.Router() is like a modular sub-panel of umpires for one ground.

Example

javascript
const express = require('express');
const app = express();

app.get('/products/:id', (req, res) => {
  const { id } = req.params;
  res.json({ productId: id, name: 'Sample Product' });
});

app.get('/search', (req, res) => {
  const { q } = req.query;
  res.json({ query: q, results: [] });
});

app.listen(3000);

Output

A GET request to /products/42 returns {"productId":"42","name":"Sample Product"}. A GET request to /search?q=laptop returns {"query":"laptop","results":[]}. Express extracts :id into req.params.id and query string values into req.query automatically.

🏏

Cricket analogy: A GET to /matches/42 returns {"matchId":"42","venue":"Sample Ground"} like a scorecard pulled up by match number; a GET to /search?q=finals returns {"query":"finals","results":[]} with Express automatically extracting :id into req.params.id and the search term into req.query.

Key Takeaways

  • Routes combine an HTTP method and a path pattern with a handler function.
  • Route parameters (e.g. :id) are accessed via req.params; query strings via req.query.
  • express.Router() lets you group related routes into modular, mountable route handlers.
  • app.all() matches a path for all HTTP methods, useful for logging or authentication checks.

Practice what you learned

Was this page helpful?

Topics covered

#NodeJs#NodeJsExpressStudyNotes#WebDevelopment#RoutingInExpress#Routing#Express#Syntax#Explanation#StudyNotes#SkillVeris