After a catalogue API moves from Express 4 to 5, app.all('*') throws a TypeError at startup and an optional :format? route breaks; what changed, and how do you fix each route?
answer
- new path parser
- fails loudly at registration
- captures change shape
- root no longer matched
basics
~20 sExpress 5 parses route strings with path-to-regexp v8, which throws on bare *, ? and inline regex when the route is registered. Name the wildcard (/{*splat} includes the root), use braces for optional parts, then fix handlers reading the new captures.
solid answer
~40 sExpress 5 routes through path-to-regexp v8, and it rejects Express 4 syntax at **registration**: `app.all('*')` throws `TypeError: Missing parameter name at index 1: *`, `/:format?` throws `Unexpected ?`, and `:id(\d+)` throws `Unexpected (`. That is the loud half: rewrite them as `/{*splat}` for a catch-all that includes `/`, `{/:format}` or `{.:format}` for optional parts, and a `RegExp` route or an array of paths for constraints. The quiet half is in handlers: a wildcard capture is now an array of segments, `req.params[0]` no longer exists for string wildcards, an absent optional parameter is omitted rather than `undefined`, `/*splat` does not match the root, and for string paths `req.params` has a null prototype. Run the test suite and watch `DEBUG=router,router:*` to see what matches.
code
javascript · 20 linesconst express = require('express');
const app = express();
// Express 4: app.get('/catalogue/:file.:format?', ...)
app.get('/catalogue/:file{.:format}', (req, res) => {
const format = Object.hasOwn(req.params, 'format') ? req.params.format : 'json';
res.json({ file: req.params.file, format });
});
// Express 4: app.get('/assets/*', ...) read req.params[0]
app.get('/assets/*path', (req, res) => {
res.json({ path: req.params.path.join('/') });
});
// Express 4: app.all('*', ...) as the fallback
app.all('/{*splat}', (req, res) => {
res.status(404).json({ error: 'not found' });
});
app.listen(3000);go deeper
Recall that Express 5 rejects bare *, ? and inline regex in route strings, and that wildcards now need a name.
Explain how each old path maps to the v8 syntax and how wildcard arrays and omitted params change what handlers receive.
Show a safe upgrade plan: let registration errors surface in tests, audit handlers for silent changes, and use the router debug namespace to confirm matching.
Plan the upgrade across many services: shared route conventions, test coverage that loads every router, and how to sequence the change without a long dual-version period.
## What changed underneath Express 5 moved routing into the separate `router` package, which compiles string route paths with **path-to-regexp v8**. Express 4 used a much older version that accepted regex-like syntax inside strings. Version 8 removed it on purpose: the library's documentation says an effort was made to make ambiguous paths from earlier releases throw, so "you might be seeing an error when things worked before". An upgrade therefore breaks routes in two different ways, and they need different hunting. ## The loud half: registration throws The parser runs when `app.get`, `app.all`, `app.use` or a router method is **called**, not when a request arrives. A path with old syntax throws a `TypeError` (path-to-regexp's `PathError`) during startup, and the message names the problem, its index and the path: | Express 4 path | Express 5 error | Express 5 replacement | | --- | --- | --- | | `'*'` or `'/*'` | `Missing parameter name at index 1: *` | `'/{*splat}'` (includes `/`) or `'/*splat'` | | `'/:file.:format?'` | `Unexpected ? at index ...` | `'/:file{.:format}'` | | `'/items/:id(\\d+)'` | `Unexpected ( at index ...` | a `RegExp` route, or validate in code | | `'/(discussion\|page)/:slug'` | `Unexpected ( at index ...` | `['/discussion/:slug', '/page/:slug']` | | `'/ab+cd'` | `Unexpected + at index 3: /ab+cd` | a `RegExp` route such as `/^\/ab+cd$/` | Every message ends with a pointer to the library's error guide. Because the throw happens at boot, a broken route takes the whole process down before it serves a single request; that is helpful in CI and painful if routes are registered lazily, so make sure every router module is loaded by at least one test. ## The quiet half: handlers read different values Once the paths parse, several behaviours change without any error: - **Wildcards capture arrays.** On `/assets/*path`, a request for `/assets/img/a.png` gives `req.params.path` the value `['img', 'a.png']`. Code that called `.toLowerCase()` or `.startsWith()` on it now throws; join it with `'/'` first. - **No numeric keys for string wildcards.** Express 4's `'/*'` stored the capture in `req.params[0]`. A named wildcard is stored under its name; numeric keys now come only from `RegExp` routes. - **Absent optionals are omitted.** In Express 4 an unmatched optional parameter had a key set to `undefined` and an unmatched wildcard was `''`. In Express 5 the key is missing, so a check such as `'format' in req.params` changes its answer. - **`/*splat` needs at least one segment.** A single-page fallback written as `'/*splat'` no longer serves `/`. Use `'/{*splat}'` when the root must match. - **`req.params` has a null prototype** for string paths, so `req.params.hasOwnProperty('id')` throws "is not a function". Use `Object.hasOwn(req.params, 'id')`. ## Working through the upgrade 1. Install Express 5 and start the app or run the tests: each bad path throws on registration and names itself. 2. Rewrite each path with the table above, keeping the slash inside braces only when it is part of what may be missing. 3. Search handlers for `req.params[0]`, string methods on wildcard values, `hasOwnProperty` on `req.params` and checks for `undefined` parameters. 4. Re-run the tests and add cases for the root path, an absent optional part and a multi-segment wildcard. 5. When a request still lands on the wrong route, start the app with `DEBUG=router,router:*`; Express 5 logs routing under the `router` namespace, not under `express:router` as Express 4 did. ## Tests worth adding A route table that parsed is not yet a route table that behaves. Cover the cases the new syntax changes: - The root path for every catch-all, since `'/*splat'` and `'/{*splat}'` differ only there. - A request with and without each optional part, asserting on the response rather than on `req.params` internals. - A multi-segment request on each wildcard route, so array handling is exercised. - An encoded segment such as `%20` in a parameter, to confirm the handler receives the decoded value. ## Things that did not change Registration order still decides which route wins, `RegExp` route paths still work, `:name` still matches one segment, and the query string is still ignored by routing. Two other Express 5 changes often surface during the same upgrade but are not routing questions: rejected promises from handlers now reach the error middleware, and `req.body` and `req.query` behave differently; treat them separately. ## Why the strictness is worth it The old syntax let a typo silently create a route that matched far more, or far less, than intended. Making ambiguity a startup error turns those bugs into failures you see before deploying, at the cost of one careful pass over the route table.
- In Express 5, why does a fallback route registered as '/*splat' not serve the root path?A wildcard must match at least one segment, and the slash before it is a literal, so `/` has nothing for `*splat` to capture. Wrapping the whole thing in braces, `'/{*splat}'`, makes the slash and the wildcard optional together; on `/` the `splat` key is then simply absent from `req.params`.
- How do you see which routes an Express 5 app matched for a request?Start it with `DEBUG=router,router:*` (add `express:*` for application logs). Express 5 delegates routing to the `router` package, which logs layer creation and dispatch under those namespaces; the Express 4 namespace `express:router` no longer produces those lines.
- Does a RegExp route need rewriting when moving to Express 5?Usually not. A real `RegExp` object is still accepted and matched as written, with unnamed groups in numeric keys of `req.params` and named groups under their names. Only regex syntax inside string paths was removed.
- Express 5 still accepts app.all('*') as a catch-all.
- The upgrade only fails at request time, so startup proves the routes are fine.
- A wildcard capture is still a string, so string methods keep working on it.
- '/*splat' matches the root path just like Express 4's '/*' did.
- An absent optional parameter still appears in req.params as undefined.
- RegExp route objects must be rewritten for Express 5.
Assumes Express 5 with path-to-regexp v8; Express 4 accepted *, ? and inline regex, stored wildcards as strings and kept unmatched params as keys.