Scalable REST API built with Express + MongoDB, featuring JWT authentication, role-based access control, and Tasks CRUD.
- API versioning:
/api/v1/* - Authentication: register, login, logout, get profile
- Password hashing with
bcryptjs - JWT auth via httpOnly cookie
- Role-based access (
user,admin) - Tasks CRUD for secondary entity
- Request validation and sanitization (
express-validator) - Centralized error handling middleware
- Swagger documentation at
/api-docs
- Node.js, Express
- MongoDB + Mongoose
- JWT + Cookies
- Swagger (OpenAPI)
src/Controllerbusiness logicsrc/Router/v1versioned API routessrc/Middlewareauth, role, validation, errorssrc/ModelsMongoDB schemassrc/Validatorsrequest validatorssrc/Docs/swagger.jsOpenAPI setup
- Install dependencies:
npm install
- Configure env file:
MONGO_URI=your_mongodb_connection_stringJWT_SECRET=your_long_random_secretPORT=3000CLIENT_ORIGIN=http://localhost:5173
- Start server:
- Development:
npm run dev - Production:
npm start
- Development:
POST /api/v1/auth/registerPOST /api/v1/auth/loginPOST /api/v1/auth/logoutGET /api/v1/auth/meGET /api/v1/auth/admin/users(admin only)
POST /api/v1/tasksGET /api/v1/tasksGET /api/v1/tasks/:idPATCH /api/v1/tasks/:idDELETE /api/v1/tasks/:id
- Passwords are hashed before storage.
- JWT is stored in httpOnly cookie.
- Token blacklist supports logout invalidation.
- Input validation is enforced for auth and task payloads.
Current design is modular and can scale by:
- Splitting modules into microservices (
auth-service,task-service) - Adding Redis for caching session/token metadata and hot task queries
- Introducing message queues for async workload
- Horizontal scaling behind a load balancer
- Adding structured logs and centralized monitoring
- Swagger UI:
http://localhost:3000/api-docs - Postman collection:
BackendAuthSystem.postman_collection.json