Chapter 18: Practical Examples
Web Application Development
Case: Create Blog System
18.1 Web 应用开发
bash
You: create a complete blog system including:
- RESTful API (Node.js + Express)
- MongoDB database
- JWT authentication
- CRUD operations (posts, comments)
- Unit tests
Claude: I'll create a complete blog system. Let me break this down:
[Step 1: Project Setup]
✓ Created package.json
✓ Installed dependencies
[Step 2: Database Models]
✓ models/User.js
✓ models/Post.js
✓ models/Comment.js
[Step 3: API Routes]
✓ routes/auth.js (login, register)
✓ routes/posts.js (CRUD)
✓ routes/comments.js (CRUD)
[Step 4: Middleware]
✓ middleware/auth.js (JWT verification)
✓ middleware/validation.js
[Step 5: Tests]
✓ 32 tests created and passing
[Step 6: Documentation]
✓ API documentation generated
Blog system complete! 🎉API Development
Case: E-commerce API
18.2 API 接口开发
bash
You: create e-commerce API including:
- User management
- Product catalog
- Shopping cart
- Order processing
- Payment integration (Stripe)
Claude: Creating e-commerce API...
[Architecture Design]
✓ API blueprint created
✓ Database schema designed
[Implementation]
✓ User service
✓ Product service
✓ Cart service
✓ Order service
✓ Payment service (Stripe integration)
[API Endpoints]
POST /api/auth/register
POST /api/auth/login
GET /api/products
POST /api/cart/add
POST /api/orders
POST /api/payments/charge
[Tests & Documentation]
✓ 45 integration tests
✓ OpenAPI documentation
E-commerce API ready! 🛒Code Review and Refactoring
Case: Legacy Code Refactoring
18.3 代码审查与重构
bash
You: refactor legacy/app.js, a 1500-line file,
needs:
- Split into modules
- Apply design patterns
- Improve testability
- Maintain backward compatibility
Claude: Analyzing legacy code...
[Analysis]
- 1,523 lines of code
- Multiple responsibilities mixed
- No tests
- Tight coupling
[Refactoring Plan]
1. Extract routes → routes/
2. Extract services → services/
3. Extract utilities → utils/
4. Apply dependency injection
5. Create tests
[Execution]
✓ Refactored into 12 modules
✓ Applied MVC pattern
✓ Created 68 unit tests
✓ All existing functionality preserved
✓ 100% backward compatible
Refactoring complete!
Before: 1,523 lines, 1 file
After: 850 lines, 12 files
Test coverage: 92%Automation Scripts
Case: CI/CD Pipeline
18.4 自动化脚本
bash
You: create complete CI/CD pipeline:
- GitHub Actions
- Automated tests
- Code quality checks
- Docker builds
- Automated deployment
Claude: Creating CI/CD pipeline...
✓ .github/workflows/ci.yml
✓ .github/workflows/deploy.yml
✓ Dockerfile
✓ docker-compose.yml
✓ deployment scripts
Pipeline stages:
1. Lint & Format check
2. Run tests
3. Build Docker image
4. Security scan
5. Deploy to staging
6. Run smoke tests
7. Deploy to production
Pipeline ready! 🚀Test-Driven Development
Case: TDD Practice
18.5 测试驱动开发
bash
You: develop user registration feature using TDD
Claude: Starting TDD approach...
[Red Phase - Write failing tests]
✓ test/auth.test.js created
- should register new user
- should validate email format
- should hash password
- should prevent duplicate email
- should return JWT token
[Green Phase - Implement minimum code]
✓ src/auth.js implemented
All tests passing ✓
[Refactor Phase - Improve code]
✓ Extracted validation logic
✓ Improved error handling
✓ Added JSDoc comments
TDD cycle complete!
Tests: 5/5 passing
Coverage: 100%Summary
In this chapter, we learned through practical examples:
- ✅ Web application development (Blog system)
- ✅ API development (E-commerce API)
- ✅ Code review and refactoring (Legacy code)
- ✅ Automation scripts (CI/CD pipeline)
- ✅ Test-driven development (TDD practice)
Key Takeaways:
- Claude Code handles complex real-world projects
- Complete workflow support from planning to implementation
- Automated testing and documentation generation
- Best practices application
Next Step: The final chapter is Resources!