A production-ready Express.js backend template with TypeScript, featuring robust authentication, security measures, and best practices.
- TypeScript Support: Full TypeScript integration with proper configurations
- Authentication:
- JWT-based authentication with access and refresh tokens
- Cookie-based token storage
- Password hashing with bcrypt
- Role-based authorization middleware
- Security:
- Helmet for security headers
- Rate limiting for API endpoints
- MongoDB sanitization
- CORS configuration
- Cookie security
- Input validation using Zod
- API Documentation:
- Swagger/OpenAPI documentation
- Auto-generated API docs
- Error Handling:
- Global error handling
- Custom API error class
- Validation error handling
- TypeScript type checking
- Logging:
- Morgan for HTTP request logging
- Winston for application logging
- Different log levels for development/production
- Code Quality:
- ESLint configuration
- Prettier code formatting
- Import sorting
- TypeScript strict mode
- Database:
- MongoDB with Mongoose
- Type-safe models
- Database connection handling
- Development Tools:
- Hot reloading with ts-node-dev
- Environment variable validation
- Path aliases (@/ imports)
- Proper development scripts
- Node.js (v22.11.0 or higher)
- MongoDB
- Yarn package manager
- Clone the repository:
git clone <repository-url>
cd backend- Install dependencies:
yarn install- Set up environment variables:
cp .env.example .env- Configure your
.envfile with appropriate values:
NODE_ENV=development
PORT=8086
MONGO_URI=mongodb://localhost:27017/your-db-name
ACCESS_TOKEN_SECRET=your-access-token-secret
REFRESH_TOKEN_SECRET=your-refresh-token-secret
HASH_SECRET=your-hash-secret
OTP_SECRET=your-otp-secret- Development:
yarn dev # Start development server with hot-reload- Production:
yarn build # Build the project
yarn start # Start production server- Code Quality:
yarn lint # Run ESLint
yarn lint:fix # Fix ESLint issues
yarn format # Format code with Prettier
yarn format:check # Check code formattingOnce the server is running, access the Swagger documentation at:
http://localhost:8086/api/v1/docs
docs/ # Swagger API definitions
src/
├── config/ # Configuration files
├── controllers/ # Route controllers
├── middlewares/ # Express middlewares
├── models/ # Mongoose models
├── routes/ # API routes
├── services/ # Business logic
├── types/ # TypeScript types
├── utils/ # Utility functions
├── validations/ # Request validations
├── app.ts # Express app setup
└── server.ts # Server entry point
POST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- Login userPOST /api/v1/auth/logout- Logout userGET /api/v1/auth/status- Check auth status
-
Rate Limiting:
- Login: 3 requests per hour
- Registration: 10 requests per hour
- Other endpoints: Customizable limits
-
Input Validation:
- Zod schema validation for all requests
- Custom password strength validation
- Email format validation
- Phone number format validation
-
Security Headers:
- CORS configuration
- Helmet security headers
- MongoDB query sanitization
The template includes a global error handler that:
- Converts all errors to a consistent format
- Handles validation errors
- Provides stack traces in development
- Logs errors appropriately
Feel free to submit issues and enhancement requests!
This project is licensed under the MIT License.