A comprehensive, production-ready Express.js backend boilerplate with TypeScript, featuring JWT authentication, file storage, email services, background jobs, and more.
- JWT Authentication - Stateless authentication with access and refresh tokens
- Role-Based Access Control - Flexible role system for authorization
- Password Reset & Email Verification - Complete user authentication flow
- Rate Limiting - Redis-backed rate limiting for API endpoints
- Strict Auth Rate Limits - 5 login attempts per 15min, 3 password resets per hour
- Input Sanitization - MongoDB injection and XSS protection
- Security Headers - Helmet with custom CSP configuration
- Dual Storage Modes - S3 or local disk storage
- Signed URLs - S3 pre-signed URLs for secure file access
- File Streaming - Direct file streaming for local storage
- User-Owned Files - Files scoped to uploading user
- File Validation - Type and size restrictions via environment config
- Nodemailer Integration - SMTP email sending
- Email Templates - Password reset, verification, and welcome emails
- Background Processing - Bull queue for async email sending
- Retry Logic - Automatic retry on email failures
- MongoDB with Mongoose - Full ODM with schemas and validations
- Zod Schema Validation - Request validation with type safety
- Connection Management - Graceful connection handling
- Indexes - Optimized queries with proper indexing
- Bull Queues - Redis-backed job queuing
- Email Worker - Async email processing
- Job Retry - Exponential backoff retry strategy
- Queue Monitoring - Job statistics and health checks
- Pino Logger - High-performance JSON logging
- Log Rotation - Daily rotation with 30-day retention
- Request Logging - Automatic HTTP request/response logging
- Error Tracking - Comprehensive error logging
- OpenAPI 3.0 - Auto-generated from Zod schemas
- Swagger UI - Interactive API documentation
- Type-Safe Routes - Validated requests and responses
- Modular Structure - Feature-based organization
- Clean Architecture - Router β Controller β Service β Repository layers
- TypeScript - Full type safety throughout
- Path Aliases - Clean imports with
@/prefix
- Runtime: Node.js (LTS)
- Framework: Express.js
- Language: TypeScript
- Database: MongoDB with Mongoose
- Cache/Queue: Redis with ioredis & Bull
- Authentication: JWT with jsonwebtoken
- Validation: Zod
- File Storage: AWS S3 SDK / Local filesystem
- Email: Nodemailer
- Logging: Pino with rotation
- Testing: Vitest
- Code Quality: Biome
- Documentation: OpenAPI 3.0 / Swagger
- Node.js (LTS version)
- MongoDB (local or cloud)
- Redis (local or cloud)
- SMTP server credentials (for email)
- AWS S3 credentials (optional, for S3 storage)
npm installdocker compose up -dThis starts MongoDB and Redis containers.
Copy .env.example to .env and configure:
cp .env.example .envRequired configurations:
MONGO_URL- MongoDB connection stringREDIS_URL- Redis connection stringJWT_SECRET- Secret key for JWT tokensSMTP_*- SMTP server credentials
Optional configurations:
UPLOAD_STORAGE_TYPE-localors3(default: local)- AWS credentials (if using S3)
- Email verification settings
npm run devServer will start on http://localhost:5000
POST /register- Register new userPOST /login- Login and get tokensPOST /refresh- Refresh access tokenPOST /logout- Logout and revoke tokensPOST /forgot-password- Request password resetPOST /reset-password- Reset password with tokenPOST /verify-email- Verify email addressPOST /send-verification- Resend verification email
GET /- Get all users (protected)GET /:id- Get user by IDPATCH /:id- Update userDELETE /:id- Delete user
POST /upload- Upload file (protected)GET /- Get user's files (protected)GET /:id- Get file metadataGET /:id/download- Download file (S3 URL or stream)DELETE /:id- Delete file (protected, user-owned)
GET /- Get all todosGET /:id- Get todo by IDPOST /- Create todoPATCH /:id- Update todoDELETE /:id- Delete todo
GET /- Health check endpoint
See .env.example for complete list. Key variables:
# Server
NODE_ENV=development
PORT=5000
CORS_ORIGIN=http://localhost:5000
# MongoDB
MONGO_URL=mongodb://localhost:27017
DB_NAME=good2order_dev
# Redis
REDIS_URL=redis://localhost:6379
# JWT
JWT_SECRET=your-secret-key
JWT_ACCESS_EXPIRATION=15m
JWT_REFRESH_EXPIRATION=7d
# Email (SMTP)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-app-password
EMAIL_FROM=Good2Order <noreply@good2order.com>
# File Upload
UPLOAD_STORAGE_TYPE=local
UPLOAD_MAX_FILE_SIZE=5242880
UPLOAD_LOCAL_PATH=./uploads
# AWS S3 (if using S3 storage)
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_S3_BUCKET=your-bucket# Run tests
npm test
# Run tests with coverage
npm run test:coverage# Build
npm run build
# Start production server
npm startnpm run dev- Start development server with hot reloadnpm run build- Build for productionnpm start- Run production servernpm test- Run testsnpm run lint- Check code qualitynpm run lint:fix- Fix linting issuesnpm run format- Format code with Biome
backend/
βββ src/
β βββ api/ # API modules
β β βββ user/ # User & auth
β β β βββ userModel.ts
β β β βββ userSchema.ts
β β β βββ userRepository.ts
β β β βββ userService.ts
β β β βββ userController.ts
β β β βββ userRouter.ts
β β β βββ authService.ts
β β β βββ authController.ts
β β β βββ authRouter.ts
β β βββ files/ # File management
β β βββ todo/ # Example CRUD
β βββ common/
β β βββ middleware/ # Express middleware
β β β βββ authentication.ts
β β β βββ authRateLimiter.ts
β β β βββ upload.ts
β β β βββ ...
β β βββ services/ # Shared services
β β β βββ email/
β β β βββ storage/
β β β βββ queue/
β β βββ utils/ # Utilities
β β βββ redis.ts
β β βββ db.ts
β β βββ envConfig.ts
β βββ api-docs/ # OpenAPI docs
β βββ index.ts # App entry point
β βββ server.ts # Express setup
βββ logs/ # Log files (rotated daily)
βββ uploads/ # Local file uploads
βββ docker-compose.yml # MongoDB & Redis
βββ .env.example # Environment template
βββ package.json
- JWT Tokens stored in Redis for revocation
- Password Hashing with bcrypt (10 rounds)
- Rate Limiting on all endpoints
- Input Validation with Zod schemas
- MongoDB Injection prevention with sanitization
- XSS Protection with input cleaning
- Security Headers with Helmet
- CORS configured for specific origins
docker build -t good2order-backend .
docker run -p 5000:5000 --env-file .env good2order-backendEnsure production environment variables are set:
- Use strong
JWT_SECRET - Configure proper
CORS_ORIGIN - Set
NODE_ENV=production - Use production MongoDB/Redis instances
- Configure SMTP for emails
This boilerplate is perfect for:
- SaaS applications
- Mobile app backends
- API-first applications
- Multi-user platforms
- Content management systems
- E-commerce backends
MIT
π Happy coding!