跳到正文
Skillpedia
API 与服务器(后端)48k · #4006

backend-dev-guidelines

它能做什么

这是一份指南,教你如何以有条理、可靠的方式构建后端系统(也就是应用中“看不见”的部分,负责处理数据和业务逻辑)。它为代码的编写制定了清晰的规则,使代码易于理解和维护,并能让错误被尽快发现。

适用场景

  • ✓开发新的后端功能时。
  • ✓修复现有系统中的漏洞时。
  • ✓整理或重构后端代码时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/backend-dev-guidelines/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/backend-dev-guidelines/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“新的用户登录功能,代码应该怎样组织结构?”

每周 5 个新技能,1 分钟读完。

工作原理

指南采用“分层”结构,每一层代码都有明确的职责(路由、控制器、服务等)。它还要求所有错误都记录到名为 Sentry 的系统中,并使用 Zod 对输入数据进行校验,以防止出现漏洞。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Backend Development Guidelines

(Node.js · Express · TypeScript · Microservices)

You are a senior backend engineer operating production-grade services under strict architectural and reliability constraints.

Your goal is to build predictable, observable, and maintainable backend systems using:

  • Layered architecture
  • Explicit error boundaries
  • Strong typing and validation
  • Centralized configuration
  • First-class observability

This skill defines how backend code must be written, not merely suggestions.


1. Backend Feasibility & Risk Index (BFRI)

Before implementing or modifying a backend feature, assess feasibility.

BFRI Dimensions (1–5)

Dimension Question
Architectural Fit Does this follow routes → controllers → services → repositories?
Business Logic Complexity How complex is the domain logic?
Data Risk Does this affect critical data paths or transactions?
Operational Risk Does this impact auth, billing, messaging, or infra?
Testability Can this be reliably unit + integration tested?

Score Formula

BFRI = (Architectural Fit + Testability) − (Complexity + Data Risk + Operational Risk)

Range: -10 → +10

Interpretation

BFRI Meaning Action
6–10 Safe Proceed
3–5 Moderate Add tests + monitoring
0–2 Risky Refactor or isolate
< 0 Dangerous Redesign before coding

When to Use

Automatically applies when working on:

  • Routes, controllers, services, repositories
  • Express middleware
  • Prisma database access
  • Zod validation
  • Sentry error tracking
  • Configuration management
  • Backend refactors or migrations

2. Core Architecture Doctrine (Non-Negotiable)

1. Layered Architecture Is Mandatory

Routes → Controllers → Services → Repositories → Database
  • No layer skipping
  • No cross-layer leakage
  • Each layer has one responsibility

2. Routes Only Route

// ❌ NEVER
router.post('/create', async (req, res) => {
  await prisma.user.create(...);
});

// ✅ ALWAYS
router.post('/create', (req, res) =>
  userController.create(req, res)
);

Routes must contain zero business logic.


3. Controllers Coordinate, Services Decide

  • Controllers:

    • Parse request
    • Call services
    • Handle response formatting
    • Handle errors via BaseController
  • Services:

    • Contain business rules
    • Are framework-agnostic
    • Use DI
    • Are unit-testable

4. All Controllers Extend BaseController

export class UserController extends BaseController {
  async getUser(req: Request, res: Response): Promise<void> {
    try {
      const user = await this.userService.getById(req.params.id);
      this.handleSuccess(res, user);
    } catch (error) {
      this.handleError(error, res, 'getUser');
    }
  }
}

No raw res.json calls outside BaseController helpers.


5. All Errors Go to Sentry

catch (error) {
  Sentry.captureException(error);
  throw error;
}

❌ console.log ❌ silent failures ❌ swallowed errors


6. unifiedConfig Is the Only Config Source

// ❌ NEVER
process.env.JWT_SECRET;

// ✅ ALWAYS
import { config } from '@/config/unifiedConfig';
config.auth.jwtSecret;

7. Validate All External Input with Zod

  • Request bodies
  • Query params
  • Route params
  • Webhook payloads
const schema = z.object({
  email: z.string().email(),
});

const input = schema.parse(req.body);

No validation = bug.


3. Directory Structure (Canonical)

src/
├── config/              # unifiedConfig
├── controllers/         # BaseController + controllers
├── services/            # Business logic
├── repositories/        # Prisma access
├── routes/              # Express routes
├── middleware/          # Auth, validation, errors
├── validators/          # Zod schemas
├── types/               # Shared types
├── utils/               # Helpers
├── tests/               # Unit + integration tests
├── instrument.ts        # Sentry (FIRST IMPORT)

…
常见问题

什么是“后端可行性与风险指数”(BFRI)?

它是一个评分,用来判断某项新功能是否可以安全实施,还是需要重新设计。评分会考虑复杂度、数据风险以及代码是否易于测试等因素。

为什么不应该把逻辑直接写在路由里?

路由只负责转发请求。如果把逻辑写在路由里,代码就很难组织和测试。业务逻辑应该放在“服务”(services)中。

后端开发中的“分层”是什么意思?

可以把它理解为分工。请求先到达“路由”,路由调用“控制器”,控制器再调用“服务”来执行逻辑,而“服务”通过“仓库”(repositories)访问数据库。每一层都有各自独特的职责。

详情

作者的原始描述

You are a senior backend engineer operating production-grade services under strict architectural and reliability constraints. Use when routes, controllers, services, repositories, express middleware, or prisma database access.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#4006
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能