| name | express |
| description | Express.js middleware patterns, routing, error handling, security, and production best practices. |
| allowed-tools | Read, Write, Edit, Bash, Glob, Grep |
| graph | {"domains":["domain:web-development"],"specializations":["specialization:web-development"],"skillAreas":["skill-area:backend-api-design","skill-area:middleware-design"],"roles":["role:backend-engineer","role:fullstack-engineer"],"topics":["topic:rest","topic:api-design"]} |
Express Skill
Expert assistance for building Node.js APIs with Express.js.
Capabilities
- Configure Express applications with middleware
- Implement RESTful routing patterns
- Handle errors with custom middleware
- Apply security best practices
- Set up validation and parsing
- Configure production deployments
Usage
Invoke this skill when you need to:
- Build REST APIs with Express
- Implement middleware pipelines
- Handle errors gracefully
- Add authentication/authorization
- Set up API documentation
Inputs
| Parameter | Type | Required | Description |
|---|
| routePath | string | Yes | Route path prefix |
| methods | array | Yes | HTTP methods |
| middleware | array | No | Middleware to apply |
| validation | boolean | No | Add validation |
Patterns
Application Setup
import express, { Express, Request, Response, NextFunction } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';
import morgan from 'morgan';
import { rateLimit } from 'express-rate-limit';
import { errorHandler, notFoundHandler } from './middleware/error';
import { usersRouter } from './routes/users';
import { authRouter } from './routes/auth';
export function createApp(): Express {
const app = express();
app.use(helmet());
app.use(cors({
origin: process.env.CORS_ORIGIN || 'http://localhost:3000',
credentials: true,
}));
app.(({
: * * ,
: ,
: ,
: ,
}));
app.(express.({ : }));
app.(express.({ : , : }));
app.(());
app.((process.. === ? : ));
app.(, authRouter);
app.(, usersRouter);
app.(, {
res.({ : , : ().() });
});
app.(notFoundHandler);
app.(errorHandler);
app;
}
Router with Controllers
import { Router } from 'express';
import { UsersController } from '../controllers/users.controller';
import { authenticate, authorize } from '../middleware/auth';
import { validate } from '../middleware/validate';
import { createUserSchema, updateUserSchema } from '../schemas/user.schema';
const router = Router();
const controller = new UsersController();
router.get('/', authenticate, controller.findAll);
router.get('/:id', authenticate, controller.findById);
router.post('/', authenticate, authorize('admin'), validate(createUserSchema), controller.create);
router.put('/:id', authenticate, validate(updateUserSchema), controller.update);
router.delete('/:id', authenticate, authorize('admin'), controller.delete);
export { router as usersRouter };
{ , , } ;
{ } ;
{
service = ();
findAll = (: , : , : ) => {
{
{ page = , limit = , search } = req.;
users = ..({
: (page),
: (limit),
: search ,
});
res.(users);
} (error) {
(error);
}
};
findById = (: , : , : ) => {
{
user = ..(req..);
(!user) {
res.().({ : });
}
res.(user);
} (error) {
(error);
}
};
create = (: , : , : ) => {
{
user = ..(req.);
res.().(user);
} (error) {
(error);
}
};
update = (: , : , : ) => {
{
user = ..(req.., req.);
res.(user);
} (error) {
(error);
}
};
= (: , : , : ) => {
{
..(req..);
res.().();
} (error) {
(error);
}
};
}
Middleware Patterns
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
export interface AuthRequest extends Request {
user?: {
id: string;
email: string;
role: string;
};
}
export function authenticate(req: AuthRequest, res: Response, next: NextFunction) {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) {
return res.status(401).json({ error: 'Authentication required' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as AuthRequest['user'];
req. = decoded;
();
} {
res.().({ : });
}
}
() {
{
(!req. || !roles.(req..)) {
res.().({ : });
}
();
};
}
{ , , } ;
{ , } ;
() {
{
{
schema.(req.);
();
} (error) {
(error ) {
res.().({
: ,
: error.,
});
}
(error);
}
};
}
{ , , } ;
{
() {
(message);
}
}
() {
res.().({ : });
}
() {
.(err);
(err ) {
res.(err.).({ : err. });
}
res.().({
: process.. ===
?
: err.,
});
}
Async Handler Wrapper
import { Request, Response, NextFunction, RequestHandler } from 'express';
type AsyncRequestHandler = (
req: Request,
res: Response,
next: NextFunction
) => Promise<any>;
export function asyncHandler(fn: AsyncRequestHandler): RequestHandler {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
}
router.get('/', asyncHandler(async (req, res) => {
const users = await usersService.findAll();
res.json(users);
}));
Best Practices
- Use middleware for cross-cutting concerns
- Implement proper error handling
- Validate all inputs
- Apply security middleware (helmet, cors, rate limit)
- Structure code with controllers and services
Target Processes
- nodejs-api-development
- rest-api-development
- mern-stack-development
- backend-architecture