A comprehensive REST API backend for campus management systems with role-based access control, supporting students, faculty, and administrators.
- Features
- Tech Stack
- Quick Start
- API Documentation
- Authentication
- Project Structure
- Environment Variables
- Development
- Deployment
- Contributing
- License
- JWT-based authentication with http-only cookies
- API Key system for additional security layer
- Role-based access control (RBAC) with three user roles
- Input validation using express-validator
- CORS protection with configurable origins
- Multi-role system: Admin, Faculty, Student
- User registration with profile image upload
- Secure login/logout with token management
- Profile management and user data retrieval
- Course management with materials and enrollments
- Attendance tracking with session-based records
- Results management with bulk operations
- Announcements with file attachments
- Event management with scheduling
- Notification system for real-time updates
- Modular architecture with clean separation of concerns
- File upload support with multer and Cloudinary integration
- Database optimization with MongoDB and Mongoose
- Error handling with custom API error classes
- Development tools with nodemon and prettier
| Category | Technology |
|---|---|
| Runtime | Node.js 18+ |
| Framework | Express.js 5.x |
| Database | MongoDB 6+ with Mongoose |
| Authentication | JWT, bcryptjs |
| Validation | express-validator |
| File Upload | multer, Cloudinary |
| Development | nodemon, prettier |
- Node.js 18 or higher
- MongoDB 6 or higher
- npm or yarn package manager
git clone <repository-url>
cd campusHub
npm installcp .env.example .env
# Edit .env with your configuration# Using Docker for MongoDB (recommended)
docker compose up -d
# Start the application
npm run devThe server will start on http://localhost:4000
curl http://localhost:4000/api/v1/healthCheckhttp://localhost:4000/api/v1
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /users/register |
Register new user | β |
| POST | /users/login |
User login | β |
| GET | /users/me |
Get user profile | β |
| POST | /users/api-key |
Generate API key | β |
| Method | Endpoint | Description | Roles |
|---|---|---|---|
| GET | /announcements |
List announcements | All |
| POST | /announcements |
Create announcement | Admin, Faculty |
| Method | Endpoint | Description | Roles |
|---|---|---|---|
| GET | /courses |
List courses | All |
| POST | /courses |
Create course | Admin |
| GET | /courses/:id/materials |
Get course materials | Faculty, Student |
| POST | /courses/:id/materials |
Add materials | Faculty |
| Method | Endpoint | Description | Roles |
|---|---|---|---|
| POST | /attendance |
Create attendance | Admin, Faculty |
| GET | /attendance/user/:userId |
Get user attendance | All |
| GET | /attendance/course/:courseId |
Get course attendance | Admin, Faculty |
| Method | Endpoint | Description | Roles |
|---|---|---|---|
| POST | /results |
Create results | Admin |
| GET | /results/:studentId |
Get student results | All |
| Method | Endpoint | Description | Roles |
|---|---|---|---|
| GET | /events |
List events | All |
| POST | /events |
Create event | Admin, Faculty |
| PUT | /events/:id |
Update event | Admin, Faculty |
| DELETE | /events/:id |
Delete event | Admin, Faculty |
- Register/Login β Receive JWT in http-only cookie
- Generate API Key β Get API key for protected endpoints
- Access Resources β Use both JWT cookie and API key header
## User Roles
| Role | Permissions |
| ----------- | ---------------------------------------------------- |
| **Admin** | Full system access, user management, course creation |
| **Faculty** | Course materials, attendance, announcements, events |
| **Student** | View courses, materials, results, announcements |
## π Project Structure
campusHub/
βββ src/
β βββ app.js # Express app configuration
β βββ index.js # Server entry point
β βββ constants.js # Application constants
β βββ controllers/ # Request handlers
β β βββ user.controller.js
β β βββ course.controller.js
β β βββ ...
β βββ middlewares/ # Custom middlewares
β β βββ auth.middleware.js
β β βββ multer.middleware.js
β β βββ validator.middleware.js
β βββ models/ # Mongoose models
β β βββ user.model.js
β β βββ course.model.js
β β βββ ...
β βββ routes/ # API routes
β β βββ user.routes.js
β β βββ course.routes.js
β β βββ ...
β βββ services/ # Business logic
β βββ utils/ # Utility functions
β βββ validators/ # Input validation
βββ public/
β βββ temp/ # Temporary file storage
βββ docker-compose.yml # MongoDB container
βββ package.json
βββ README.md
Create a .env file in the project root:
# Server Configuration
PORT=4000
NODE_ENV=development
CORS_ORIGIN=http://localhost:3000
# Database
MONGO_URI=mongodb://localhost:27017
# JWT Secrets
ACCESS_TOKEN_SECRET=your-super-secret-access-token-key
REFRESH_TOKEN_SECRET=your-super-secret-refresh-token-key
# Cloudinary (Optional)
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret
npm run dev # Start development server with nodemon
npm start # Start production serverdocker compose up -d# Install MongoDB locally
# Update MONGO_URI in .env to: mongodb://localhost:27017{
"statusCode": 200,
"data": {
// Response data
},
"message": "Success",
"success": true
}{
"statusCode": 400,
"message": "Validation error",
"success": false,
"errors": [
{
"field": "email",
"message": "Invalid email format"
}
]
}-
MongoDB Connection Error
- Ensure MongoDB is running
- Check MONGO_URI in .env file
- Verify network connectivity
-
JWT Token Issues
- Check ACCESS_TOKEN_SECRET is set
- Ensure cookies are enabled in client
- Verify token expiration
-
File Upload Problems
- Check multer configuration
- Verify public/temp directory exists
- Ensure file size limits are appropriate
DEBUG=campusHub:* npm run devThis project is licensed under the ISC License - see the LICENSE file for details.
Sumit Singh
- GitHub: @sumit-si
- Email: [email protected]
β Star this repository if you found it helpful!
Made with β€οΈ