Skill 寫法圖鑑

nodejs-backend-patterns

wshobson | 架構與重構 | 原文 | MIT

用 Express/Fastify 建構可上線的 Node.js 後端服務

中文翻譯

---
name: nodejs-backend-patterns
description: 用 Express/Fastify 建構可上線的 Node.js 後端服務,實作 middleware 模式、錯誤處理、驗證授權、資料庫整合與 API 設計最佳實務。在建立 Node.js 伺服器、REST API、GraphQL 後端或 microservices 架構時使用。
---

# Node.js Backend Patterns

用現代框架、架構模式與最佳實務,建構可擴展、好維護、可上線的 Node.js 後端應用的完整指引。

## When to Use This Skill

- 建構 REST API 或 GraphQL 伺服器
- 用 Node.js 建立 microservices
- 實作驗證與授權
- 設計可擴展的後端架構
- 設定 middleware 與錯誤處理
- 整合資料庫(SQL 與 NoSQL)
- 用 WebSockets 建構即時應用
- 實作背景工作處理

## Detailed patterns and worked examples

詳細的模式文件放在 `references/details.md`。上面的導覽層不夠用時,再去讀那個檔案。

## Best Practices

1. **使用 TypeScript**:型別安全能預防執行期錯誤
2. **實作適當的錯誤處理**:使用自訂的錯誤 class
3. **驗證輸入**:用 Zod 或 Joi 這類函式庫
4. **使用環境變數**:絕對不要把機密寫死在程式碼裡
5. **實作 logging**:使用結構化 logging(Pino、Winston)
6. **加上 rate limiting**:防止濫用
7. **使用 HTTPS**:正式環境一律要用
8. **正確實作 CORS**:正式環境不要用 `*`
9. **使用 dependency injection**:更好測試與維護
10. **寫測試**:單元、整合與 E2E 測試
11. **處理 graceful shutdown**:清理資源
12. **使用 connection pooling**:用於資料庫
13. **實作 health check**:用於監控
14. **使用壓縮**:縮小回應大小
15. **監控效能**:使用 APM 工具

## Testing Patterns

完整的測試指引請見 `javascript-testing-patterns` skill。

英文原文

---
name: nodejs-backend-patterns
description: Build production-ready Node.js backend services with Express/Fastify, implementing middleware patterns, error handling, authentication, database integration, and API design best practices. Use when creating Node.js servers, REST APIs, GraphQL backends, or microservices architectures.
---

# Node.js Backend Patterns

Comprehensive guidance for building scalable, maintainable, and production-ready Node.js backend applications with modern frameworks, architectural patterns, and best practices.

## When to Use This Skill

- Building REST APIs or GraphQL servers
- Creating microservices with Node.js
- Implementing authentication and authorization
- Designing scalable backend architectures
- Setting up middleware and error handling
- Integrating databases (SQL and NoSQL)
- Building real-time applications with WebSockets
- Implementing background job processing

## Detailed patterns and worked examples

Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.

## Best Practices

1. **Use TypeScript**: Type safety prevents runtime errors
2. **Implement proper error handling**: Use custom error classes
3. **Validate input**: Use libraries like Zod or Joi
4. **Use environment variables**: Never hardcode secrets
5. **Implement logging**: Use structured logging (Pino, Winston)
6. **Add rate limiting**: Prevent abuse
7. **Use HTTPS**: Always in production
8. **Implement CORS properly**: Don't use `*` in production
9. **Use dependency injection**: Easier testing and maintenance
10. **Write tests**: Unit, integration, and E2E tests
11. **Handle graceful shutdown**: Clean up resources
12. **Use connection pooling**: For databases
13. **Implement health checks**: For monitoring
14. **Use compression**: Reduce response size
15. **Monitor performance**: Use APM tools

## Testing Patterns

See `javascript-testing-patterns` skill for comprehensive testing guidance.