Route Matching and Request Processing: How Web Frameworks Work
🎯 Introduction
By the end of this article, you will be able to define endpoints by method and path and run shared processing before them.
🧭 Send each request to its endpoint
Routing passes requests to the appropriate handler. In Express, declare rules with methods such as app.get and app.post; the framework performs matching.
Create routing-app.mjs:
import express from 'express';
export const app = express();
Exporting the app lets both the startup file and tests use it.
Log the request before handling the endpoint
To log each request's method and path, add middleware before the routes:
app.use((req, res, next) => {
console.log(req.method, req.path);
next();
});
next() passes the request to subsequent handlers. If a handler neither responds nor calls next, the request keeps waiting. Logging the path without query parameters avoids recording sensitive query values.
User list endpoint
Add the list endpoint:
app.get('/users', (req, res) => {
res.json({ data: [{ id: 1, name: 'Ming' }] });
});
GET /users runs this callback and returns 200 with a user array. POST /users does not match, while GET /users?name=Ming still does. The handler uses query parameters separately.
Health check endpoint
Add another endpoint in the same file:
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
It returns 200 and {"status":"ok"} to check whether the HTTP service responds. It does not access the database, so it cannot establish database health.
🔀 Matching and code order
For GET /users, the logging middleware runs first. After next, the user route matches and ends the response by sending JSON. The framework looks up registered methods and paths internally; our code defines endpoint behavior.
To add authentication, place the check before business processing. Return 401 on failure and continue only on success. See Express Routing for routing APIs, dynamic route ordering, and splitting routes with Router.
🚫 Handle unmatched requests after the routes
If neither route matches, return a consistent 404:
app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found' });
});
This must follow the routes; otherwise, it ends requests too early. POST /users is undefined here and also returns 404. Express does not automatically generate 405 for known paths. Configure allowed methods separately if your API contract requires 405.
🚀 Start and verify
Create routing-server.mjs solely to start listening:
import { app } from './routing-app.mjs';
app.listen(3000, '127.0.0.1');
Run node routing-server.mjs, then request the user endpoint:
curl -i http://127.0.0.1:3000/users
The response is 200 with the user list, and the server prints GET /users. /health returns ok, while /missing returns 404.
❓ Questions and Answers
Q1: Does adding /health establish database health?
No. This example returns a fixed result. A database health check must actually check its connection.
Q2: What if the number of endpoints keeps growing?
Split them by feature, such as users or files, with Express Router. See Express Routing for the syntax.
Q3: Where should shared authentication checks go?
Before the routes they protect. Send an error and end processing on authentication failure; continue only on success.
🧪 Practice
Add GET /version before the 404 handler, return a version object, and confirm that unknown paths still return 404.
// Write your code here
