cc-skill-backend-patterns
构建可扩展的后端系统
后端应用通常存在架构不佳、可扩展性问题和安全漏洞。本技能提供 API 设计、数据库优化、缓存、认证和错误处理的成熟模式。
Télécharger le ZIP du skill
Importer dans Claude
Allez dans Paramètres → Capacités → Skills → Importer un skill
Activez et commencez à utiliser
Tester
Utilisation de "cc-skill-backend-patterns". 展示如何为 products 资源设计 RESTful API
Résultat attendu:
GET /api/products - 列出所有产品
GET /api/products/:id - 获取单个产品
POST /api/products - 创建产品
PUT /api/products/:id - 替换产品
PATCH /api/products/:id - 更新产品
DELETE /api/products/:id - 删除产品
查询参数:?status=active&sort=price&limit=20&offset=0
Utilisation de "cc-skill-backend-patterns". 获取带客户的订单时如何防止 N+1 查询?
Résultat attendu:
不要这样:
for (const order of orders) {
order.customer = await getCustomer(order.customerId)
}
应该这样:
const customerIds = orders.map(o => o.customerId)
const customers = await getCustomers(customerIds)
const customerMap = new Map(customers.map(c => [c.id, c]))
orders.forEach(o => o.customer = customerMap.get(o.customerId))
Utilisation de "cc-skill-backend-patterns". 创建 JWT 认证中间件
Résultat attendu:
export function verifyToken(token: string) {
try {
return jwt.verify(token, process.env.JWT_SECRET!)
} catch (error) {
throw new ApiError(401, 'Invalid token')
}
}
export async function requireAuth(request: Request) {
const token = request.headers.get('authorization')?.replace('Bearer ', '')
if (!token) throw new ApiError(401, 'Missing token')
return verifyToken(token)
}
Audit de sécurité
Risque faibleEducational backend development patterns skill. Static analyzer flagged numerous false positives due to misidentification of code comments as shell commands, environment variable access as sensitive data exposure, and URL paths as reconnaissance. True positive finding: JWT secret accessed via process.env - standard secure practice. No malicious behavior detected.
Problèmes à risque faible (1)
Facteurs de risque
🔑 Variables d’environnement (1)
Motifs détectés
Score de qualité
Ce que vous pouvez construire
设计新的 API 端点
架构师和开发人员构建新的 REST API 时,可以应用基于资源的 URL 模式和中间件方法,实现一致且可维护的 API 设计。
优化缓慢的数据库查询
遇到性能问题的开发人员可以使用批量获取和查询优化模式来识别和修复 N+1 查询问题。
实现安全认证
为应用添加认证功能的开发人员可以按照安全最佳实践实现 JWT 验证和基于角色的访问控制。
Essayez ces prompts
为 [resource] 资源设计 RESTful API 结构。包含列出、按 ID 获取、创建、更新和删除的端点。展示过滤和分页的查询参数示例。
我有一个函数先获取订单列表,然后遍历每个订单获取客户详情。这导致性能缓慢。请展示如何使用批量获取模式重构此代码以避免 N+1 查询。
展示如何实现 JWT 令牌验证和 requireAuth 中间件来保护 API 路由。包含无效令牌的错误处理。
使用 Redis 为 getUser 函数创建旁路缓存模式实现。包含缓存命中/未命中逻辑和缓存失效处理。
Bonnes pratiques
- 使用仓库层、服务层和控制器层分离关注点,实现可维护的代码
- 始终在处理前使用 Zod 等模式验证库验证输入
- 战略性地实现缓存——缓存耗时操作但要正确失效
Éviter
- 不要使用 SELECT * 查询所有列——仅指定需要的字段以提升性能
- 避免在循环中嵌套数据库调用——使用批量获取替代
- 不要将原始数据库错误暴露给客户端——使用集中式错误处理
Foire aux questions
本技能涵盖哪些语言和框架?
如何在 PUT 和 PATCH 之间选择?
何时应该使用 Redis 而非内存缓存?
如何在无服务器函数中处理认证?
后端项目的推荐结构是什么?
如何正确实现限流?
Détails du développeur
Auteur
affaan-mLicence
MIT
Dépôt
https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/cc-skill-backend-patternsRéf
main
Structure de fichiers
📄 SKILL.md