Facebook Pixel

Common Routing Mistakes in Express (and How to Avoid Them)

Express routing has predictable failure modes. Here are the common mistakes and fixes.

Common Routing Mistakes in Express

Express routing has predictable failure modes. Here are the common mistakes and how to avoid them.

Mistake 1: Routes in Wrong Order

You put a wildcard route before a specific route:

app.get('/users/*', ...);
app.get('/users/me', ...); // never reached

Fix: declare specific routes before wildcards.

Mistake 2: Forgetting next() in Middleware

A middleware does not call next. The request hangs. Fix: every middleware must call next() or send a response. No orphans.

Mistake 3: Sync Handler Without Response

A handler does work but never calls res.json or res.send. The request hangs. Fix: every handler must respond or pass to next(err).

Mistake 4: Mounting Router With Wrong Path

You mount app.use('/', usersRouter) instead of app.use('/users', usersRouter). All routes hit / instead of /users. Fix: mount with the right prefix.

Mistake 5: Same Path, Different Method Confusion

You write router.get('/:id', ...) and router.get('/me', ...). /me matches /:id first, so me is treated as id. Fix: put /me before /:id, or use a different path.

Mistake 6: Async Errors Not Caught

You use async handlers in Express 4 without a wrapper. Promise rejections are swallowed. Fix: wrap with asyncHandler that catches and calls next(err). Or upgrade to Express 5.

Mistake 7: Using GET for Non-Idempotent Actions

You use GET to delete a resource. GETs can be retried by browsers and crawlers. Fix: use POST or DELETE for non-idempotent actions.

Mistake 8: Inconsistent Status Codes

You return 200 for creates, 200 for not found, 200 for everything. Clients cannot tell success from failure. Fix: use the right status codes (201 for create, 404 for not found, 400 for bad request).

Mistake 9: No API Versioning

You ship /users. Six months later you need to change the response shape. All clients break. Fix: version from day one: /api/v1/users.

Mistake 10: Routes in app.js

Every route lives in app.js. The file is 500 lines. Fix: split routes by feature with express.Router and mount in app.js.

The Takeaway

Common Express routing mistakes: routes in wrong order, middleware without next(), handler without response, wrong mount path, same path with different methods, async errors not caught, GET for non-idempotent actions, inconsistent status codes, no versioning, and all routes in app.js. Avoid these from day one.

Routes in wrong order (wildcards first), middleware without next(), handler without response, wrong mount path, same path with different methods, async errors not caught, GET for non-idempotent actions, inconsistent status codes, no versioning, and all routes in app.js.

Probably a wildcard route is declared before them. Express matches in declaration order, so /users/* catches /users/me before /users/me can. Fix: declare specific routes before wildcards.

A middleware forgot to call next() or a handler forgot to send a response. Every middleware must call next() or respond; every handler must respond or pass to next(err). No orphans.

Because /:id was declared before /me. /:id matches any string, including 'me'. Fix: declare /me before /:id, or use a different path like /me/profile.

Express 4 does not catch promise rejections from async handlers. Wrap with asyncHandler that catches and calls next(err). Or upgrade to Express 5, which catches async errors automatically.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.