backend-server-template
v2.12.0
Published
A production-ready Node.js/Express.js backend server template with TypeScript, MongoDB, and best practices built-in
Maintainers
Readme
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
Development:
npm run dev- Uses nodemon for auto-restart
- Runs on development environment
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
- Add Routes: Create specific route modules in
routes/ - Add Controllers: Implement business logic in
controllers/ - Add Models: Define data models in
models/ - Add Services: External service integration in
services/ - Add Middleware: Custom middleware in
middlewares/ - Add Validation: Use Zod for request validation
- Add Database: Connect to MongoDB using Mongoose
- Add Authentication: JWT implementation
- Add Testing: Unit and integration tests
- 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