Skip to content

square-story/backend-server-template

Repository files navigation

Server Architecture - Best Practices

Separation of Concerns

This project follows a clean separation of concerns pattern:

📁 File Structure

src/
├── index.ts          # Entry point
├── server.ts         # HTTP server setup & lifecycle
├── app.ts           # Express app configuration
├── config/
│   └── env.ts       # Environment configuration
├── routes/
│   └── index.ts     # Route aggregation
├── controllers/      # Business logic
├── services/        # External service integration
├── models/          # Data models
├── middlewares/     # Custom middleware
├── repositories/    # Data access layer
└── utils/           # Utility functions

🔧 Key Components

1. app.ts - Express Application Configuration

  • Purpose: Configure Express app, middleware, and routes
  • Responsibilities:
    • Security middleware (helmet, cors)
    • Body parsing
    • Logging (morgan)
    • Route registration
    • Error handling
    • Health checks

2. server.ts - HTTP Server Management

  • Purpose: Handle server lifecycle and process management
  • Responsibilities:
    • Start HTTP server
    • Graceful shutdown handling
    • Process signal handling (SIGTERM, SIGINT)
    • Uncaught exception handling
    • Unhandled promise rejection handling

3. index.ts - Application Entry Point

  • Purpose: Clean entry point that imports server
  • Benefits:
    • Single responsibility
    • Easy testing
    • Clear dependency chain

🚀 Benefits of This Separation

1. Testability

// Easy to test app configuration without starting server
import app from './app';
import request from 'supertest';

describe('App', () => {
  it('should return health status', async () => {
    const response = await request(app).get('/health');
    expect(response.status).toBe(200);
  });
});

2. Flexibility

  • Can run app without server (for testing)
  • Can run server with different configurations
  • Easy to swap HTTP server implementation

3. Maintainability

  • Clear separation of concerns
  • Easy to understand what each file does
  • Modular architecture

4. Production Ready

  • Graceful shutdown handling
  • Proper error handling
  • Environment-specific configuration
  • Security middleware

🔄 Development Workflow

  1. Development: npm run dev

    • Uses nodemon for auto-restart
    • Runs on development environment
  2. Production: npm run build && npm start

    • Compiles TypeScript
    • Runs on production environment

📝 Best Practices Implemented

Security

  • ✅ Helmet for security headers
  • ✅ CORS configuration
  • ✅ Environment-based configuration
  • ✅ Input validation (ready for Zod)

Error Handling

  • ✅ Global error handler
  • ✅ 404 handler
  • ✅ Uncaught exception handling
  • ✅ Graceful shutdown

Monitoring

  • ✅ Health check endpoint
  • ✅ Request logging (development)
  • ✅ Environment logging

Code Organization

  • ✅ Separation of concerns
  • ✅ Modular architecture
  • ✅ Clean entry point
  • ✅ TypeScript support

🎯 Next Steps

  1. Add Routes: Create specific route modules in routes/
  2. Add Controllers: Implement business logic in controllers/
  3. Add Models: Define data models in models/
  4. Add Services: External service integration in services/
  5. Add Middleware: Custom middleware in middlewares/
  6. Add Validation: Use Zod for request validation
  7. Add Database: Connect to MongoDB using Mongoose
  8. Add Authentication: JWT implementation
  9. Add Testing: Unit and integration tests
  10. Add Documentation: API documentation with Swagger

🔧 Environment Variables

Create .env.development and .env.production files:

PORT=8000
NODE_ENV=development
JWT_SECRET=your-secret-key
MONGO_URI=mongodb://localhost:27017/your-db
ALLOWED_ORIGINS=http://localhost:3000

About

A TypeScript-Express backend template for scalable, clean-architecture server projects.

Resources

Stars

Watchers

Forks

Packages

No packages published