Express routers and service boundaries
Framework lane: Express 5.x (Supertest required for integration test). Exact supported patch, dependency lock, advisory review and execution are NOT RUN. The framework code is a static candidate; do not install dependencies or expose a server automatically. The local companion uses Node.js 22.16.0 and requires no third-party packages.
Framework example — staged
import express from 'express';export function createApp(repository) { const app = express(); const router = express.Router(); const findItem = id => repository.find(id); router.get('/:id', async (req, res) => { const item = await findItem(req.params.id); if (!item) return res.sendStatus(404); res.json({ id: item.id, title: item.title }); }); app.use('/catalog', router); return app;}Expected behavior (not observed): With a repository that resolves {id:"p1",title:"Notebook"}, GET /catalog/p1 should return those fields. A missing item should return 404.
How it works
Keep the transport boundary thin: extract validated input, invoke a service, and map results into HTTP. A repository provides access to records. An app factory creates isolated application state for tests and can use a different repository without changing routes. Do not invent separate layers merely to rename functions; use a boundary when dependencies or policy need separation.
Practice the underlying idea without dependencies
const makeCatalog = repo => id => { const item = repo.find(id); return item ? { id: item.id, title: item.title } : null;};const find = makeCatalog({find:id=>id==='p1'?{id,title:'Notebook',cost:2}:null});console.log(JSON.stringify([find('p1'),find('missing')]));Run node main.mjs in a disposable directory. Expected output:
[{"id":"p1","title":"Notebook"},null]This tests the illustrated JavaScript behavior only, not the framework.
Common mistake
Constructing a shared mutable repository in module scope makes isolation and state ownership harder. Inject the repository into the app factory, and explicitly map public fields.
Apply it yourself
Create two catalog services with different in-memory datasets and verify their results remain independent.
Further reading
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Express routers and service boundaries — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
Which layer should decide the public HTTP status?
Why pass the repository to createApp?
What is the effective route after mounting /:id under /catalog?