Skip to content
Back to skills

Express Middleware Design

ASecurity

Orders Express and Fastify middleware correctly, centralizes error handling and validates requests at the edge, so handlers only see clean input.

  • 3 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added November 15, 2026
ai-agentsrustexpresssecurity

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill express-middleware-design --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Express Middleware Design?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Express Middleware Design
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-express-middleware-design/badge)](https://www.skillsdirectory.com/skills/workspace-templates-express-middleware-design)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: express-middleware-design
description: Orders Express and Fastify middleware correctly, centralizes error handling and validates requests at the edge, so handlers only see clean input.
---

# Express middleware design

Treat the middleware stack as a pipeline with a fixed order. Read the entry file and every `app.use` call before editing.

## Order, outside in

1. Request id and structured logging.
2. Security headers (`helmet`) and CORS with an explicit origin list.
3. Body parsers with a size limit (`express.json({ limit: '100kb' })`).
4. Rate limiting on auth and write routes.
5. Authentication, then authorization per route.
6. Validation of `params`, `query` and `body` with a schema library such as zod, before any handler runs.
7. Route handlers.
8. A 404 handler, then one error handler with four arguments, registered last.

## Steps

1. Wrap async handlers so rejections reach `next(err)`. Express 5 does this itself; on Express 4 add a small `asyncHandler` wrapper. In Fastify, throw and let the error handler catch it.
2. Define an `AppError` class carrying `status`, a stable `code` and a safe message. Handlers throw it; they never call `res.status(500)` themselves.
3. In the error handler, map `AppError` and validation errors to their status, log everything else with the request id, and answer 500 with a generic body. Never send stack traces.
4. Validate once, write the parsed result back to `req`, and type it so handlers trust it.

## Rules

- Never call `next()` after sending a response.
- Do not put business logic in middleware; it parses, checks and passes on.
- Keep each middleware in its own file with a single job.

## Report

Show the final stack order, the files changed, and routes that still lack validation.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.