A production-ready Express.js backend boilerplate built with TypeScript, MongoDB, and Mongoose following industry best practices and SOLID principles.
- β TypeScript - Full type safety and modern JavaScript features
- β Express.js - Fast, unopinionated web framework
- β MongoDB & Mongoose - Robust database with ODM
- β Error Handling - Centralized error handling middleware
- β Validation - Request validation with express-validator
- β Security - Helmet, CORS, Rate Limiting
- β Logging - Custom logger with different log levels
- β Environment Config - dotenv for configuration management
- β Code Quality - ESLint with TypeScript support
- β Hot Reload - Nodemon for development
src/
βββ config/ # Configuration files (database, AWS, storage)
βββ controllers/ # HTTP request/response handlers
βββ middlewares/ # Custom middleware functions
βββ models/ # Mongoose models and schemas
βββ repositories/ # Data access layer (Repository Pattern)
βββ routes/ # API route definitions
βββ services/ # Business logic layer
βββ types/ # TypeScript type definitions
βββ utils/ # Utility functions and helpers
βββ app.ts # Express app setup
βββ server.ts # Server entry point
This project follows a clean, layered architecture:
- Routes β Define endpoints and validation
- Controllers β Handle HTTP requests/responses
- Services β Implement business logic
- Repositories β Abstract database operations
- Models β Define data structure
See ARCHITECTURE.md for detailed architecture documentation.
For detailed setup instructions, see GETTING_STARTED.md
# Run automated setup script
./setup-new-project.sh my-new-project
cd my-new-project
cp .env.example .env
# Edit .env with your settings
npm run devgit clone <repository-url>
cd Sinux-Boilerplate
npm install
cp .env.example .env
# Edit .env with your settings
npm run devπ See GETTING_STARTED.md for complete step-by-step guide
- Start MongoDB
# Make sure MongoDB is running on your system
mongod- Run the development server
npm run devThe server will start on http://localhost:5000
npm run dev- Start development server with hot reloadnpm run build- Build TypeScript to JavaScriptnpm start- Run production server
npm run lint- Run ESLintnpm run lint:fix- Fix ESLint errorsnpm run format- Format code with Prettiernpm run format:check- Check code formatting
npm test- Run testsnpm run test:watch- Run tests in watch mode
npm run start:pm2- Start with PM2npm run stop:pm2- Stop PM2 processesnpm run restart:pm2- Restart PM2 processesnpm run delete:pm2- Delete from PM2npm run logs:pm2- View PM2 logsnpm run monit:pm2- Monitor PM2 processes
GET /health- Server health status
GET /api/v1/examples- Get all examplesGET /api/v1/examples/:id- Get example by IDPOST /api/v1/examples- Create new examplePUT /api/v1/examples/:id- Update exampleDELETE /api/v1/examples/:id- Delete example
POST /api/v1/upload/single- Upload single filePOST /api/v1/upload/multiple- Upload multiple files (max 10)DELETE /api/v1/upload/delete- Delete fileGET /api/v1/upload/metadata- Get file metadata
- Repository Pattern for data access abstraction
- Service Layer for business logic
- Controller Layer for HTTP handling
- Clear separation of concerns
- Easy to test and maintain
- Centralized error handling middleware
- Custom AppError class for operational errors
- Async error handling with asyncHandler wrapper
- Mongoose error handling (ValidationError, CastError, etc.)
- Proper connection error handling
- Event listeners for connection states
- Graceful shutdown on process termination
- Repository pattern for query abstraction
- Development: Local storage
- Production: AWS S3
- Automatic switching based on NODE_ENV
- Image optimization with Sharp
- File validation and size limits
- Helmet for security headers
- CORS configuration
- Rate limiting to prevent abuse
- Input validation and sanitization
- Environment variable protection
- Husky for git hooks
- Prettier for code formatting
- ESLint for linting
- TypeScript for type safety
- Pre-commit and pre-push hooks
- PM2 for process management
- Cluster mode for scalability
- Graceful shutdown handling
- Auto-restart on crashes
- Log management
- Request validation using express-validator
- Custom validation middleware
- Schema-level validation with Mongoose
- Consistent API response structure
- Success and error response helpers
- Proper HTTP status codes
| Variable | Description | Default |
|---|---|---|
| NODE_ENV | Environment mode | development |
| PORT | Server port | 5000 |
| MONGODB_URI | MongoDB connection string | mongodb://localhost:27017/sinux-boilerplate |
| JWT_SECRET | JWT secret key | - |
| JWT_EXPIRE | JWT expiration time | 7d |
| CORS_ORIGIN | Allowed CORS origin | http://localhost:3000 |
| RATE_LIMIT_WINDOW_MS | Rate limit window | 900000 |
| RATE_LIMIT_MAX_REQUESTS | Max requests per window | 100 |
See DEPLOYMENT.md for detailed deployment instructions to AWS Lightsail with PM2.
# Build application
npm run build
# Start with PM2
npm run start:pm2
# View logs
npm run logs:pm2curl -X POST http://localhost:5000/api/v1/upload/single \
-F "file=@/path/to/image.jpg"curl -X POST http://localhost:5000/api/v1/upload/multiple \
-F "files=@/path/to/image1.jpg" \
-F "files=@/path/to/image2.jpg"This project uses Husky for automated git hooks:
- pre-commit: Runs lint-staged (ESLint + Prettier on staged files)
- commit-msg: Validates commit message format (Conventional Commits)
- pre-push: Runs linting and build to ensure code quality
All commits must follow the Conventional Commits specification:
<type>(<scope>): <subject>
# Examples:
git commit -m "feat(auth): add JWT refresh token"
git commit -m "fix(validation): correct email regex"
git commit -m "docs: update API documentation"See COMMITS.md for commit format. Use npm run commit for interactive helper.
npm run prepare- Create a feature branch
- Make your changes
- Husky will automatically run checks on commit/push
- Submit a pull request
DOCS.md - Documentation index
Quick Links:
- GETTING_STARTED.md - Setup guide
- ARCHITECTURE.md - Architecture
- AGENTS.md - Development guidelines
- COMMITS.md - Commit format
- DEPLOYMENT.md - Deployment
This project is designed to be a reusable boilerplate for future Express.js + TypeScript + MongoDB projects!
# Copy to new project
cp -r Sinux-Boilerplate My-New-Project
cd My-New-Project
# Initialize
npm install
cp .env.example .env
npm run dev# Make script executable
chmod +x setup-new-project.sh
# Create new project
./setup-new-project.sh my-awesome-apiSee BOILERPLATE_GUIDE.md for complete instructions.
- β Production-ready architecture
- β Clean code patterns (SOLID principles)
- β File uploads (S3/Local)
- β Validation system
- β Error handling
- β Security (Helmet, CORS, Rate Limiting)
- β PM2 deployment ready
- β Comprehensive documentation
Save 2-3 days of setup time! π
We welcome contributions! Please read our Contributing Guide before submitting pull requests.
ISC