express-middleware
Express.js middleware patterns and implementation
What this skill does
# Express Middleware Skill
Patterns for creating and using middleware in Express.js.
## Middleware Basics
### Middleware Structure
```typescript
import { Request, Response, NextFunction } from 'express'
// Basic middleware
function middleware(req: Request, res: Response, next: NextFunction) {
// Do something
next() // Pass to next middleware
}
// Middleware with error
function middlewareWithError(req: Request, res: Response, next: NextFunction) {
if (!req.headers.authorization) {
return next(new Error('No authorization header'))
}
next()
}
// Async middleware
async function asyncMiddleware(req: Request, res: Response, next: NextFunction) {
try {
const data = await fetchSomething()
req.data = data
next()
} catch (error) {
next(error)
}
}
```
### Middleware Order
```typescript
import express from 'express'
const app = express()
// Order matters! Middleware runs top to bottom
// 1. Body parsing (run early)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// 2. Logging (run early)
app.use(morgan('dev'))
// 3. Security headers
app.use(helmet())
// 4. CORS
app.use(cors())
// 5. Authentication (before routes)
app.use(authenticate)
// 6. Routes
app.use('/api', routes)
// 7. 404 handler (after routes)
app.use(notFoundHandler)
// 8. Error handler (last)
app.use(errorHandler)
```
## Request Processing
### Request Logging
```typescript
function requestLogger(req: Request, res: Response, next: NextFunction) {
const start = Date.now()
// Log on response finish
res.on('finish', () => {
const duration = Date.now() - start
console.log({
method: req.method,
path: req.path,
status: res.statusCode,
duration: `${duration}ms`,
ip: req.ip,
})
})
next()
}
```
### Request ID
```typescript
import { v4 as uuid } from 'uuid'
function requestId(req: Request, res: Response, next: NextFunction) {
const id = req.headers['x-request-id'] || uuid()
req.id = id as string
res.setHeader('X-Request-ID', id)
next()
}
```
### Rate Limiting
```typescript
import rateLimit from 'express-rate-limit'
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per window
message: { error: 'Too many requests, please try again later' },
standardHeaders: true,
legacyHeaders: false,
})
app.use('/api', limiter)
// Different limits for different routes
const strictLimiter = rateLimit({
windowMs: 60 * 1000,
max: 5,
})
app.use('/api/auth/login', strictLimiter)
```
## Validation
### Body Validation
```typescript
import { z } from 'zod'
function validateBody(schema: z.ZodSchema) {
return (req: Request, res: Response, next: NextFunction) => {
const result = schema.safeParse(req.body)
if (!result.success) {
return res.status(400).json({
error: 'Validation failed',
details: result.error.flatten().fieldErrors,
})
}
req.body = result.data
next()
}
}
// Usage
const createUserSchema = z.object({
name: z.string().min(1).max(100),
email: z.string().email(),
password: z.string().min(8),
})
router.post('/users', validateBody(createUserSchema), createUser)
```
### Query Validation
```typescript
function validateQuery(schema: z.ZodSchema) {
return (req: Request, res: Response, next: NextFunction) => {
const result = schema.safeParse(req.query)
if (!result.success) {
return res.status(400).json({
error: 'Invalid query parameters',
details: result.error.flatten().fieldErrors,
})
}
req.query = result.data
next()
}
}
const paginationSchema = z.object({
page: z.coerce.number().int().positive().default(1),
limit: z.coerce.number().int().min(1).max(100).default(10),
sort: z.enum(['asc', 'desc']).default('desc'),
})
router.get('/posts', validateQuery(paginationSchema), listPosts)
```
## Authentication
### JWT Authentication
```typescript
import jwt from 'jsonwebtoken'
interface JWTPayload {
userId: string
email: string
role: string
}
function authenticate(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers.authorization
if (!authHeader?.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing token' })
}
const token = authHeader.substring(7)
try {
const payload = jwt.verify(token, process.env.JWT_SECRET!) as JWTPayload
req.user = payload
next()
} catch (error) {
return res.status(401).json({ error: 'Invalid token' })
}
}
// Optional authentication
function optionalAuth(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers.authorization
if (!authHeader?.startsWith('Bearer ')) {
return next() // Continue without user
}
const token = authHeader.substring(7)
try {
const payload = jwt.verify(token, process.env.JWT_SECRET!) as JWTPayload
req.user = payload
} catch (error) {
// Invalid token, continue without user
}
next()
}
```
### Role-Based Access
```typescript
function requireRole(...roles: string[]) {
return (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
return res.status(401).json({ error: 'Unauthorized' })
}
if (!roles.includes(req.user.role)) {
return res.status(403).json({ error: 'Forbidden' })
}
next()
}
}
// Usage
router.delete('/users/:id', authenticate, requireRole('admin'), deleteUser)
router.get('/reports', authenticate, requireRole('admin', 'manager'), getReports)
```
## Error Handling
### Error Handler
```typescript
import { Request, Response, NextFunction } from 'express'
interface AppError extends Error {
statusCode?: number
code?: string
details?: unknown
}
function errorHandler(
error: AppError,
req: Request,
res: Response,
next: NextFunction
) {
console.error('Error:', {
message: error.message,
stack: error.stack,
path: req.path,
method: req.method,
})
const statusCode = error.statusCode || 500
const code = error.code || 'INTERNAL_ERROR'
res.status(statusCode).json({
error: {
code,
message: error.message,
...(process.env.NODE_ENV === 'development' && { stack: error.stack }),
},
})
}
```
### Async Handler Wrapper
```typescript
type AsyncHandler = (
req: Request,
res: Response,
next: NextFunction
) => Promise<void>
function asyncHandler(fn: AsyncHandler) {
return (req: Request, res: Response, next: NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next)
}
}
// Usage
router.get('/users', asyncHandler(async (req, res) => {
const users = await userService.findAll()
res.json({ data: users })
}))
```
### 404 Handler
```typescript
function notFoundHandler(req: Request, res: Response) {
res.status(404).json({
error: {
code: 'NOT_FOUND',
message: `Route ${req.method} ${req.path} not found`,
},
})
}
// Use after all routes
app.use(notFoundHandler)
```
## Response Helpers
### Response Wrapper
```typescript
function wrapResponse(req: Request, res: Response, next: NextFunction) {
res.success = function(data: unknown, status = 200) {
return this.status(status).json({ success: true, data })
}
res.error = function(message: string, status = 400, code?: string) {
return this.status(status).json({
success: false,
error: { code, message },
})
}
next()
}
// Extend Response type
declare global {
namespace Express {
interface Response {
success(data: unknown, status?: number): Response
error(message: string, status?: number, code?: string): Response
}
}
}
// Usage
router.get('/users/:id', asyncHandler(async (req, res) => {
const user = await userService.findById(req.params.id)
if (!user) {
return res.error('User not found', 404, 'USER_NOT_FOUND')
}
res.success(user)
}))
```
## Middleware Factory
```typescript
interface MiddlewareOptions {
logger?: boolean
caRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.