RESTful API backend for the Fleet Management System built with Express.js, MongoDB, and Node.js.
- Node.js v18 or higher
- MongoDB (local or MongoDB Atlas)
- npm or yarn package manager
- Install dependencies
npm install- Configure environment
cp .env.example .envEdit .env and configure your environment variables (see .env.example for details).
- Start development server
npm run devThe API will be available at http://localhost:4000.
For production deployment, see DEPLOYMENT.md for comprehensive instructions.
npm install --production
npm startbackend/
├── src/
│ ├── config/ # Configuration files (DB, env, queue, socket)
│ ├── constants/ # Application constants
│ ├── controllers/ # Request handlers
│ ├── middlewares/ # Express middlewares
│ ├── models/ # Mongoose schemas
│ ├── repositories/ # Data access layer
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ ├── templates/ # Email templates
│ ├── utils/ # Helper utilities
│ ├── validations/ # Request validation schemas
│ ├── workers/ # Background job workers
│ ├── app.js # Express app setup
│ └── server.js # Entry point
├── scripts/ # Utility scripts (seeding, etc.)
├── .env # Environment variables (not in git)
├── .env.example # Environment template
├── package.json # Dependencies and scripts
└── DEPLOYMENT.md # Deployment guide
npm run dev- Start development server with nodemonnpm start- Start production servernpm run seed- Seed database with sample datanpm run seed:reset- Reset and seed databasenpm run lint- Run ESLintnpm run lint:fix- Fix ESLint issuesnpm run format- Format code with Prettiernpm test- Run tests
- Multi-tenancy - Company-based data isolation
- Role-based access control - Admin, Owner, Manager, Driver roles
- JWT Authentication - Access and refresh tokens
- Real-time updates - Socket.io integration
- Background jobs - Bull queue with Redis (optional)
- Email notifications - SendGrid or SMTP support
- Rate limiting - Protection against abuse
- Validation - Joi schema validation
- Error handling - Centralized error middleware
- Audit logging - Track important actions
http://localhost:4000/api/v1
Most endpoints require authentication via JWT token in the Authorization header:
Authorization: Bearer <your_access_token>
- Auth:
/api/v1/auth- Login, register, refresh tokens - Users:
/api/v1/users- User management - Vehicles:
/api/v1/vehicles- Vehicle CRUD operations - Drivers:
/api/v1/drivers- Driver profiles - Trips:
/api/v1/trips- Trip management - Routes:
/api/v1/routes- Route planning - Maintenance:
/api/v1/maintenance- Maintenance logs - Clients:
/api/v1/clients- Client management - Analytics:
/api/v1/analytics- Dashboard analytics - Admin:
/api/v1/admin- Admin operations
curl -X GET http://localhost:4000/api/v1/vehicles \
-H "Authorization: Bearer YOUR_TOKEN"- Company - Multi-tenant organization
- User - System users with roles
- DriverProfile - Driver-specific information
- Vehicle - Fleet vehicles
- Trip - Vehicle trips/journeys
- Route - Planned routes
- MaintenanceLog - Vehicle maintenance records
- Client - Customer/client information
- AuditLog - System audit trail
- Helmet.js for security headers
- CORS configuration
- Rate limiting on auth endpoints
- JWT token-based authentication
- Password hashing with Argon2
- Input validation with Joi
- MongoDB injection protection
See .env.example for all available environment variables.
PORT- Server portMONGO_URI- MongoDB connection stringACCESS_TOKEN_SECRET- JWT access token secretREFRESH_TOKEN_SECRET- JWT refresh token secretACCESS_TOKEN_EXPIRES_IN- Token expiration (e.g., 15m)REFRESH_TOKEN_EXPIRES_IN- Token expiration (e.g., 7d)FRONTEND_URL- Frontend application URL
# Check MongoDB URI
echo $MONGO_URI
# Test connection
node -e "require('mongoose').connect(process.env.MONGO_URI).then(() => console.log('Connected')).catch(e => console.error(e))"# Find process using port 4000
lsof -i :4000 # Mac/Linux
netstat -ano | findstr :4000 # Windows
# Kill the process or change PORT in .envnpm testFor detailed deployment instructions, including:
- Platform-specific guides (Heroku, Railway, Docker)
- Production environment setup
- Security best practices
- Monitoring and logging
See DEPLOYMENT.md
- Follow the existing code structure
- Use ESLint and Prettier configurations
- Add tests for new features
- Update documentation
The application follows a layered architecture:
Controllers → Services → Repositories → Models
↓ ↓ ↓
HTTP ← Business Logic → Database
- Controllers: Handle HTTP requests/responses
- Services: Implement business logic
- Repositories: Data access and queries
- Models: Mongoose schemas
For deployment questions, refer to DEPLOYMENT.md. For setup instructions, see SETUP_COMPANY.md.
Built with: Node.js, Express, MongoDB, Socket.io, Bull, JWT