name: Node.js后端模式 description: 构建生产就绪的Node.js后端服务,使用Express/Fastify,实现中间件模式、错误处理、认证、数据库集成和API设计最佳实践。在创建Node.js服务器、REST API、GraphQL后端或微服务架构时使用。
Node.js 后端模式
提供构建可扩展、可维护、生产就绪的Node.js后端应用程序的全面指导,涵盖现代框架、架构模式和最佳实践。
何时使用此技能
- 构建REST API或GraphQL服务器
- 使用Node.js创建微服务
- 实现认证和授权
- 设计可扩展的后端架构
- 设置中间件和错误处理
- 集成数据库(SQL和NoSQL)
- 构建实时应用程序与WebSockets
- 实现后台作业处理
核心框架
Express.js - 简约框架
基本设置:
import express, { Request, Response, NextFunction } from 'express';
import helmet from 'helmet';
import cors from 'cors';
import compression from 'compression';
const app = express();
// 安全中间件
app.use(helmet());
app.use(cors({ origin: process.env.ALLOWED_ORIGINS?.split(',') }));
app.use(compression());
// 请求体解析
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// 请求日志
app.use((req: Request, res: Response, next: NextFunction) => {
console.log(`${req.method} ${req.path}`);
next();
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`服务器运行在端口 ${PORT}`);
});
Fastify - 高性能框架
基本设置:
import Fastify from 'fastify';
import helmet from '@fastify/helmet';
import cors from '@fastify/cors';
import compress from '@fastify/compress';
const fastify = Fastify({
logger: {
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: { colorize: true }
}
}
});
// 插件
await fastify.register(helmet);
await fastify.register(cors, { origin: true });
await fastify.register(compress);
// 类型安全路由与模式验证
fastify.post<{
Body: { name: string; email: string };
Reply: { id: string; name: string };
}>('/users', {
schema: {
body: {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' }
}
}
}
}, async (request, reply) => {
const { name, email } = request.body;
return { id: '123', name };
});
await fastify.listen({ port: 3000, host: '0.0.0.0' });
架构模式
模式1:分层架构
结构:
src/
├── controllers/ # 处理HTTP请求/响应
├── services/ # 业务逻辑
├── repositories/ # 数据访问层
├── models/ # 数据模型
├── middleware/ # Express/Fastify中间件
├── routes/ # 路由定义
├── utils/ # 辅助函数
├── config/ # 配置
└── types/ # TypeScript类型
控制器层:
// controllers/user.controller.ts
import { Request, Response, NextFunction } from 'express';
import { UserService } from '../services/user.service';
import { CreateUserDTO, UpdateUserDTO } from '../types/user.types';
export class UserController {
constructor(private userService: UserService) {}
async createUser(req: Request, res: Response, next: NextFunction) {
try {
const userData: CreateUserDTO = req.body;
const user = await this.userService.createUser(userData);
res.status(201).json(user);
} catch (error) {
next(error);
}
}
async getUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const user = await this.userService.getUserById(id);
res.json(user);
} catch (error) {
next(error);
}
}
async updateUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
const updates: UpdateUserDTO = req.body;
const user = await this.userService.updateUser(id, updates);
res.json(user);
} catch (error) {
next(error);
}
}
async deleteUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = req.params;
await this.userService.deleteUser(id);
res.status(204).send();
} catch (error) {
next(error);
}
}
}
服务层:
// services/user.service.ts
import { UserRepository } from '../repositories/user.repository';
import { CreateUserDTO, UpdateUserDTO, User } from '../types/user.types';
import { NotFoundError, ValidationError } from '../utils/errors';
import bcrypt from 'bcrypt';
export class UserService {
constructor(private userRepository: UserRepository) {}
async createUser(userData: CreateUserDTO): Promise<User> {
// 验证
const existingUser = await this.userRepository.findByEmail(userData.email);
if (existingUser) {
throw new ValidationError('邮箱已存在');
}
// 哈希密码
const hashedPassword = await bcrypt.hash(userData.password, 10);
// 创建用户
const user = await this.userRepository.create({
...userData,
password: hashedPassword
});
// 从响应中移除密码
const { password, ...userWithoutPassword } = user;
return userWithoutPassword as User;
}
async getUserById(id: string): Promise<User> {
const user = await this.userRepository.findById(id);
if (!user) {
throw new NotFoundError('用户未找到');
}
const { password, ...userWithoutPassword } = user;
return userWithoutPassword as User;
}
async updateUser(id: string, updates: UpdateUserDTO): Promise<User> {
const user = await this.userRepository.update(id, updates);
if (!user) {
throw new NotFoundError('用户未找到');
}
const { password, ...userWithoutPassword } = user;
return userWithoutPassword as User;
}
async deleteUser(id: string): Promise<void> {
const deleted = await this.userRepository.delete(id);
if (!deleted) {
throw new NotFoundError('用户未找到');
}
}
}
仓库层:
// repositories/user.repository.ts
import { Pool } from 'pg';
import { CreateUserDTO, UpdateUserDTO, UserEntity } from '../types/user.types';
export class UserRepository {
constructor(private db: Pool) {}
async create(userData: CreateUserDTO & { password: string }): Promise<UserEntity> {
const query = `
INSERT INTO users (name, email, password)
VALUES ($1, $2, $3)
RETURNING id, name, email, password, created_at, updated_at
`;
const { rows } = await this.db.query(query, [
userData.name,
userData.email,
userData.password
]);
return rows[0];
}
async findById(id: string): Promise<UserEntity | null> {
const query = 'SELECT * FROM users WHERE id = $1';
const { rows } = await this.db.query(query, [id]);
return rows[0] || null;
}
async findByEmail(email: string): Promise<UserEntity | null> {
const query = 'SELECT * FROM users WHERE email = $1';
const { rows } = await this.db.query(query, [email]);
return rows[0] || null;
}
async update(id: string, updates: UpdateUserDTO): Promise<UserEntity | null> {
const fields = Object.keys(updates);
const values = Object.values(updates);
const setClause = fields
.map((field, idx) => `${field} = $${idx + 2}`)
.join(', ');
const query = `
UPDATE users
SET ${setClause}, updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *
`;
const { rows } = await this.db.query(query, [id, ...values]);
return rows[0] || null;
}
async delete(id: string): Promise<boolean> {
const query = 'DELETE FROM users WHERE id = $1';
const { rowCount } = await this.db.query(query, [id]);
return rowCount > 0;
}
}
模式2:依赖注入
DI容器:
// di-container.ts
import { Pool } from 'pg';
import { UserRepository } from './repositories/user.repository';
import { UserService } from './services/user.service';
import { UserController } from './controllers/user.controller';
import { AuthService } from './services/auth.service';
class Container {
private instances = new Map<string, any>();
register<T>(key: string, factory: () => T): void {
this.instances.set(key, factory);
}
resolve<T>(key: string): T {
const factory = this.instances.get(key);
if (!factory) {
throw new Error(`未注册 ${key} 的工厂`);
}
return factory();
}
singleton<T>(key: string, factory: () => T): void {
let instance: T;
this.instances.set(key, () => {
if (!instance) {
instance = factory();
}
return instance;
});
}
}
export const container = new Container();
// 注册依赖
container.singleton('db', () => new Pool({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
}));
container.singleton('userRepository', () =>
new UserRepository(container.resolve('db'))
);
container.singleton('userService', () =>
new UserService(container.resolve('userRepository'))
);
container.register('userController', () =>
new UserController(container.resolve('userService'))
);
container.singleton('authService', () =>
new AuthService(container.resolve('userRepository'))
);
中间件模式
认证中间件
// middleware/auth.middleware.ts
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { UnauthorizedError } from '../utils/errors';
interface JWTPayload {
userId: string;
email: string;
}
declare global {
namespace Express {
interface Request {
user?: JWTPayload;
}
}
}
export const authenticate = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) {
throw new UnauthorizedError('未提供令牌');
}
const payload = jwt.verify(
token,
process.env.JWT_SECRET!
) as JWTPayload;
req.user = payload;
next();
} catch (error) {
next(new UnauthorizedError('无效令牌'));
}
};
export const authorize = (...roles: string[]) => {
return async (req: Request, res: Response, next: NextFunction) => {
if (!req.user) {
return next(new UnauthorizedError('未认证'));
}
// 检查用户是否有必要角色
const hasRole = roles.some(role =>
req.user?.roles?.includes(role)
);
if (!hasRole) {
return next(new UnauthorizedError('权限不足'));
}
next();
};
};
验证中间件
// middleware/validation.middleware.ts
import { Request, Response, NextFunction } from 'express';
import { AnyZodObject, ZodError } from 'zod';
import { ValidationError } from '../utils/errors';
export const validate = (schema: AnyZodObject) => {
return async (req: Request, res: Response, next: NextFunction) => {
try {
await schema.parseAsync({
body: req.body,
query: req.query,
params: req.params
});
next();
} catch (error) {
if (error instanceof ZodError) {
const errors = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message
}));
next(new ValidationError('验证失败', errors));
} else {
next(error);
}
}
};
};
// 使用Zod
import { z } from 'zod';
const createUserSchema = z.object({
body: z.object({
name: z.string().min(1),
email: z.string().email(),
password: z.string().min(8)
})
});
router.post('/users', validate(createUserSchema), userController.createUser);
速率限制中间件
// middleware/rate-limit.middleware.ts
import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import Redis from 'ioredis';
const redis = new Redis({
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT || '6379')
});
export const apiLimiter = rateLimit({
store: new RedisStore({
client: redis,
prefix: 'rl:',
}),
windowMs: 15 * 60 * 1000, // 15分钟
max: 100, // 每个IP在窗口期内限制100个请求
message: '此IP请求过多,请稍后再试',
standardHeaders: true,
legacyHeaders: false,
});
export const authLimiter = rateLimit({
store: new RedisStore({
client: redis,
prefix: 'rl:auth:',
}),
windowMs: 15 * 60 * 1000,
max: 5, // 认证端点的更严格限制
skipSuccessfulRequests: true,
});
请求日志中间件
// middleware/logger.middleware.ts
import { Request, Response, NextFunction } from 'express';
import pino from 'pino';
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: { colorize: true }
}
});
export const requestLogger = (
req: Request,
res: Response,
next: NextFunction
) => {
const start = Date.now();
// 完成时记录响应
res.on('finish', () => {
const duration = Date.now() - start;
logger.info({
method: req.method,
url: req.url,
status: res.statusCode,
duration: `${duration}ms`,
userAgent: req.headers['user-agent'],
ip: req.ip
});
});
next();
};
export { logger };
错误处理
自定义错误类
// utils/errors.ts
export class AppError extends Error {
constructor(
public message: string,
public statusCode: number = 500,
public isOperational: boolean = true
) {
super(message);
Object.setPrototypeOf(this, AppError.prototype);
Error.captureStackTrace(this, this.constructor);
}
}
export class ValidationError extends AppError {
constructor(message: string, public errors?: any[]) {
super(message, 400);
}
}
export class NotFoundError extends AppError {
constructor(message: string = '资源未找到') {
super(message, 404);
}
}
export class UnauthorizedError extends AppError {
constructor(message: string = '未授权') {
super(message, 401);
}
}
export class ForbiddenError extends AppError {
constructor(message: string = '禁止访问') {
super(message, 403);
}
}
export class ConflictError extends AppError {
constructor(message: string) {
super(message, 409);
}
}
全局错误处理程序
// middleware/error-handler.ts
import { Request, Response, NextFunction } from 'express';
import { AppError } from '../utils/errors';
import { logger } from './logger.middleware';
export const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction
) => {
if (err instanceof AppError) {
return res.status(err.statusCode).json({
status: 'error',
message: err.message,
...(err instanceof ValidationError && { errors: err.errors })
});
}
// 记录意外错误
logger.error({
error: err.message,
stack: err.stack,
url: req.url,
method: req.method
});
// 生产环境中不泄露错误详情
const message = process.env.NODE_ENV === 'production'
? '内部服务器错误'
: err.message;
res.status(500).json({
status: 'error',
message
});
};
// 异步错误包装器
export const asyncHandler = (
fn: (req: Request, res: Response, next: NextFunction) => Promise<any>
) => {
return (req: Request, res: Response, next: NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
};
数据库模式
PostgreSQL与连接池
// config/database.ts
import { Pool, PoolConfig } from 'pg';
const poolConfig: PoolConfig = {
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
};
export const pool = new Pool(poolConfig);
// 测试连接
pool.on('connect', () => {
console.log('数据库已连接');
});
pool.on('error', (err) => {
console.error('意外数据库错误', err);
process.exit(-1);
});
// 优雅关机
export const closeDatabase = async () => {
await pool.end();
console.log('数据库连接已关闭');
};
MongoDB与Mongoose
// config/mongoose.ts
import mongoose from 'mongoose';
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI!, {
maxPoolSize: 10,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
});
console.log('MongoDB已连接');
} catch (error) {
console.error('MongoDB连接错误:', error);
process.exit(1);
}
};
mongoose.connection.on('disconnected', () => {
console.log('MongoDB已断开连接');
});
mongoose.connection.on('error', (err) => {
console.error('MongoDB错误:', err);
});
export { connectDB };
// 模型示例
import { Schema, model, Document } from 'mongoose';
interface IUser extends Document {
name: string;
email: string;
password: string;
createdAt: Date;
updatedAt: Date;
}
const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
}, {
timestamps: true
});
// 索引
userSchema.index({ email: 1 });
export const User = model<IUser>('User', userSchema);
事务模式
// services/order.service.ts
import { Pool } from 'pg';
export class OrderService {
constructor(private db: Pool) {}
async createOrder(userId: string, items: any[]) {
const client = await this.db.connect();
try {
await client.query('BEGIN');
// 创建订单
const orderResult = await client.query(
'INSERT INTO orders (user_id, total) VALUES ($1, $2) RETURNING id',
[userId, calculateTotal(items)]
);
const orderId = orderResult.rows[0].id;
// 创建订单项
for (const item of items) {
await client.query(
'INSERT INTO order_items (order_id, product_id, quantity, price) VALUES ($1, $2, $3, $4)',
[orderId, item.productId, item.quantity, item.price]
);
// 更新库存
await client.query(
'UPDATE products SET stock = stock - $1 WHERE id = $2',
[item.quantity, item.productId]
);
}
await client.query('COMMIT');
return orderId;
} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
}
}
认证与授权
JWT认证
// services/auth.service.ts
import jwt from 'jsonwebtoken';
import bcrypt from 'bcrypt';
import { UserRepository } from '../repositories/user.repository';
import { UnauthorizedError } from '../utils/errors';
export class AuthService {
constructor(private userRepository: UserRepository) {}
async login(email: string, password: string) {
const user = await this.userRepository.findByEmail(email);
if (!user) {
throw new UnauthorizedError('无效凭证');
}
const isValid = await bcrypt.compare(password, user.password);
if (!isValid) {
throw new UnauthorizedError('无效凭证');
}
const token = this.generateToken({
userId: user.id,
email: user.email
});
const refreshToken = this.generateRefreshToken({
userId: user.id
});
return {
token,
refreshToken,
user: {
id: user.id,
name: user.name,
email: user.email
}
};
}
async refreshToken(refreshToken: string) {
try {
const payload = jwt.verify(
refreshToken,
process.env.REFRESH_TOKEN_SECRET!
) as { userId: string };
const user = await this.userRepository.findById(payload.userId);
if (!user) {
throw new UnauthorizedError('用户未找到');
}
const token = this.generateToken({
userId: user.id,
email: user.email
});
return { token };
} catch (error) {
throw new UnauthorizedError('无效刷新令牌');
}
}
private generateToken(payload: any): string {
return jwt.sign(payload, process.env.JWT_SECRET!, {
expiresIn: '15m'
});
}
private generateRefreshToken(payload: any): string {
return jwt.sign(payload, process.env.REFRESH_TOKEN_SECRET!, {
expiresIn: '7d'
});
}
}
缓存策略
// utils/cache.ts
import Redis from 'ioredis';
const redis = new Redis({
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT || '6379'),
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
return delay;
}
});
export class CacheService {
async get<T>(key: string): Promise<T | null> {
const data = await redis.get(key);
return data ? JSON.parse(data) : null;
}
async set(key: string, value: any, ttl?: number): Promise<void> {
const serialized = JSON.stringify(value);
if (ttl) {
await redis.setex(key, ttl, serialized);
} else {
await redis.set(key, serialized);
}
}
async delete(key: string): Promise<void> {
await redis.del(key);
}
async invalidatePattern(pattern: string): Promise<void> {
const keys = await redis.keys(pattern);
if (keys.length > 0) {
await redis.del(...keys);
}
}
}
// 缓存装饰器
export function Cacheable(ttl: number = 300) {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
const cache = new CacheService();
const cacheKey = `${propertyKey}:${JSON.stringify(args)}`;
const cached = await cache.get(cacheKey);
if (cached) {
return cached;
}
const result = await originalMethod.apply(this, args);
await cache.set(cacheKey, result, ttl);
return result;
};
return descriptor;
};
}
API响应格式
// utils/response.ts
import { Response } from 'express';
export class ApiResponse {
static success<T>(res: Response, data: T, message?: string, statusCode = 200) {
return res.status(statusCode).json({
status: 'success',
message,
data
});
}
static error(res: Response, message: string, statusCode = 500, errors?: any) {
return res.status(statusCode).json({
status: 'error',
message,
...(errors && { errors })
});
}
static paginated<T>(
res: Response,
data: T[],
page: number,
limit: number,
total: number
) {
return res.json({
status: 'success',
data,
pagination: {
page,
limit,
total,
pages: Math.ceil(total / limit)
}
});
}
}
最佳实践
- 使用TypeScript: 类型安全防止运行时错误
- 实现适当的错误处理: 使用自定义错误类
- 验证输入: 使用Zod或Joi等库
- 使用环境变量: 切勿硬编码密钥
- 实现日志记录: 使用结构化日志(Pino、Winston)
- 添加速率限制: 防止滥用
- 使用HTTPS: 在生产中始终使用
- 正确实现CORS: 生产中不要使用
* - 使用依赖注入: 更易于测试和维护
- 编写测试: 单元、集成和端到端测试
- 处理优雅关机: 清理资源
- 使用连接池: 用于数据库
- 实现健康检查: 用于监控
- 使用压缩: 减少响应大小
- 监控性能: 使用APM工具
测试模式
参见 javascript-testing-patterns 技能以获取全面的测试指导。
资源
- Node.js最佳实践: https://github.com/goldbergyoni/nodebestpractices
- Express.js指南: https://expressjs.com/en/guide/
- Fastify文档: https://www.fastify.io/docs/
- TypeScript Node Starter: https://github.com/microsoft/TypeScript-Node-Starter