express-api
Skill2 starsChanged 4 months agoMITView on GitHub
Express.js server-side development patterns for building robust REST APIs with proper middleware chains, error handling, request validation, security hardening, and scalable project structure. Use when the task involves `Express.js`, `Express server`, `Express middleware`, `Node.js REST API`, or `Express routing`.
- Reads credentials
In this repository43 other files it gives its agents.
- accessibilitySkill
- best-practicesSkill
- brainstormingSkill
- core-web-vitalsSkill
- cypress-e2eSkill
- docker-expertSkill
- drizzle-ormSkill
- elixir-developmentSkill
- eslint-configSkill
- frontend-designSkill
- github-actionsSkill
- go-developmentSkill
- grafana-dashboardsSkill
- grpc-apiSkill
- helm-chartsSkill
- jest-testingSkill
- kubernetes-orchestrationSkill
- makefileSkill
- markdown-a11ySkill
- nothing-designSkill
- performanceSkill
- php-developmentSkill
- pinned-tagSkill
- postgresql-developmentSkill
- pr-creatorSkill
- prettier-formattingSkill
- pydanticSkill
- qwik-developmentSkill
- rails-developmentSkill
- ruby-developmentSkill
- rust-async-patternsSkill
- seoSkill
- skill-creatorSkill
- solidjs-developmentSkill
- sqlalchemySkill
- sql-optimization-patternsSkill
- swift-ios-developmentSkill
- tensorflow-mlSkill
- testing-librarySkill
- trpc-type-safetySkill
- webapp-testingSkill
- web-quality-auditSkill
- zig-developmentSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 3 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 26
Centralized Error Handling: NEVER scatter try/catch in every route.
- Very specific: line 30
Security by Default: Always use helmet, configure cors explicitly (never * in production), and apply rate limiting to public endpoints.
- Very specific: line 32
Environment Config: Use a validated config module.
- Very specific: line 34
Consistent Response Shape: Every API response should follow the same envelope: { data, error, meta }.
- Very specific: line 283
Use express.Router() to modularize routes by domain.
- Very specific: line 286
Use API versioning in the URL path (/api/v1/...).
- Distinctive: line 288
Add request ID middleware for tracing across logs.
- Very specific: line 294
DON'T use app.use(cors()) with no options — it allows all origins.
- Distinctive: line 295
DON'T put business logic in route handlers or controllers — keep it in services.
- Distinctive: line 296
DON'T return stack traces or internal error details in production responses.
- Very specific: line 297
DON'T use express.static for user-uploaded files without path sanitization.
- Very specific: line 298
DON'T use synchronous file operations (fs.readFileSync) in request handlers.
- Very specific: line 299
DON'T call res.json() or res.send() more than once per request — it causes "headers already sent" errors.
- Very specific: line 301
DON'T forget to call next() in non-terminal middleware — the request will hang.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
- Building or refactoring a REST API with Express.js.
- Setting up middleware chains for authentication, logging, or validation.
- Structuring an Express project for maintainability and testability.
- Adding security headers, rate limiting, or CORS configuration.
- Implementing centralized error handling across routes.
Critical Patterns
- Layered Architecture: Separate routes → controllers → services → repositories. Routes define endpoints, controllers handle HTTP concerns, services contain business logic, repositories handle data access.
- Centralized Error Handling: NEVER scatter
try/catchin every route. Use an async wrapper and a single error-handling middleware at the end of the middleware chain. - Validate at the Edge: Validate ALL incoming data (body, params, query) at the route level using Zod or Joi BEFORE it reaches the controller.
- Security by Default: Always use
helmet, configurecorsexplicitly (never*in production), and apply rate limiting to public endpoints. - Environment Config: Use a validated config module. NEVER access
process.envdirectly throughout the codebase. - Consistent Response Shape: Every API response should follow the same envelope:
{ data, error, meta }.
Project Structure
src/
├── config/
│ └── env.ts # Validated environment config
├── middleware/
│ ├── errorHandler.ts # Centralized error handler
│ ├── validate.ts # Request validation middleware
│ ├── auth.ts # Authentication middleware
│ └── rateLimiter.ts # Rate limiting config
├── routes/
│ ├── index.ts # Route aggregator
│ └── users.routes.ts # /users route definitions
├── controllers/
│ └── users.controller.ts # HTTP concern handling
├── services/
│ └── users.service.ts # Business logic
├── repositories/
│ └── users.repository.ts # Data access
├── errors/
│ └── AppError.ts # Custom error classes
├── utils/
│ └── asyncHandler.ts # Async error wrapper
└── app.ts # Express app setupCode Examples
App Setup with Security Middleware
import express from "express";
import helmet from "helmet";
import cors from "cors";
import { rateLimit } from "express-rate-limit";
import { errorHandler } from "./middleware/errorHandler";
import { routes } from "./routes";
const app = express();
// Security middleware — order matters
app.use(helmet());
app.use(cors({
origin: process.env.ALLOWED_ORIGINS?.split(",") ?? [],
credentials: true,
}));
app.use(rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100,
standardHeaders: true,
legacyHeaders: false,
}));
// Body parsing
app.use(express.json({ limit: "10kb" }));
app.use(express.urlencoded({ extended: true }));
// Routes
app.use("/api/v1", routes);
// Health check — outside versioned routes
app.get("/health", (_req, res) => {
res.json({ status: "ok" });
});
// Centralized error handler — MUST be last
app.use(errorHandler);
export { app };Async Error Wrapper
import { Request, Response, NextFunction, RequestHandler } from "express";
// Wraps async route handlers so thrown errors reach the error middleware
export const asyncHandler = (
fn: (req: Request, res: Response, next: NextFunction) => Promise<void>
): RequestHandler => {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
};Custom Error Classes
export class AppError extends Error {
constructor(
public readonly statusCode: number,
public readonly message: string,
public readonly isOperational = true,
) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
Error.captureStackTrace(this);
}
}
export class NotFoundError extends AppError {
constructor(resource: string) {
super(404, `${resource} not found`);
}
}
export class ValidationError extends AppError {
constructor(message: string) {
super(400, message);
}
}Centralized Error Handler
import { Request, Response, NextFunction } from "express";
import { AppError } from "../errors/AppError";
import { ZodError } from "zod";
export function errorHandler(
err: Error,
_req: Request,
res: Response,
_next: NextFunction,
): void {
// Zod validation errors
if (err instanceof ZodError) {
res.status(400).json({
error: "Validation failed",
details: err.errors.map((e) => ({
path: e.path.join("."),
message: e.message,
})),
});
return;
}
// Known operational errors
if (err instanceof AppError) {
res.status(err.statusCode).json({ error: err.message });
return;
}
// Unknown errors — don't leak internals
console.error("Unhandled error:", err);
res.status(500).json({ error: "Internal server error" });
}Request Validation with Zod
import { Request, Response, NextFunction } from "express";
import { AnyZodObject, ZodError } from "zod";
export const validate = (schema: AnyZodObject) => {
return (req: Request, _res: Response, next: NextFunction) => {
try {
schema.parse({
body: req.body,
query: req.query,
params: req.params,
});
next();
} catch (err) {
next(err); // Caught by errorHandler
}
};
};
// Usage — define schemas per route
import { z } from "zod";
export const createUserSchema = z.object({
body: z.object({
email: z.string().email(),
name: z.string().min(2).max(100),
role: z.enum(["user", "admin"]).default("user"),
}),
});Routes → Controller → Service Pattern
// routes/users.routes.ts
import { Router } from "express";
import { asyncHandler } from "../utils/asyncHandler";
import { validate } from "../middleware/validate";
import { createUserSchema } from "../schemas/user.schema";
import * as controller from "../controllers/users.controller";
const router = Router();
router.get("/", asyncHandler(controller.list));
router.get("/:id", asyncHandler(controller.getById));
router.post("/", validate(createUserSchema), asyncHandler(controller.create));
export { router as usersRouter };// controllers/users.controller.ts
import { Request, Response } from "express";
import * as userService from "../services/users.service";
export async function list(_req: Request, res: Response): Promise<void> {
const users = await userService.findAll();
res.json({ data: users });
}
export async function getById(req: Request, res: Response): Promise<void> {
const user = await userService.findById(req.params.id);
res.json({ data: user });
}
export async function create(req: Request, res: Response): Promise<void> {
const user = await userService.create(req.body);
res.status(201).json({ data: user });
}Environment Config with Validation
import { z } from "zod";
const envSchema = z.object({
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string().url(),
JWT_SECRET: z.string().min(32),
ALLOWED_ORIGINS: z.string().default("http://localhost:3000"),
});
// Validate once at startup — fail fast
export const env = envSchema.parse(process.env);Best Practices
DO
- Use
express.Router()to modularize routes by domain. - Return appropriate HTTP status codes:
201for created,204for no-content,404for not found. - Use API versioning in the URL path (
/api/v1/...). - Set
trust proxyif behind a reverse proxy (for rate limiting and IP detection). - Add request ID middleware for tracing across logs.
- Gracefully shut down: listen for
SIGTERM/SIGINT, stop accepting new connections, drain existing ones.
DON'T
- DON'T use
app.use(cors())with no options — it allows all origins. - DON'T put business logic in route handlers or controllers — keep it in services.
- DON'T return stack traces or internal error details in production responses.
- DON'T use
express.staticfor user-uploaded files without path sanitization. - DON'T use synchronous file operations (
fs.readFileSync) in request handlers. - DON'T call
res.json()orres.send()more than once per request — it causes "headers already sent" errors. - DON'T forget to call
next()in non-terminal middleware — the request will hang.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

