Skip to content

A backend Rest apis for a campus portal system where users have different roles (Student, Faculty, Admin). Each role has specific permissions to access and modify resources like announcements, results, events ,courses, enrollment , attendance and course materials.

Notifications You must be signed in to change notification settings

Sumit-Si/campusHub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🏫 CampusHub Backend

Node.js Express MongoDB License

A comprehensive REST API backend for campus management systems with role-based access control, supporting students, faculty, and administrators.

πŸ“‹ Table of Contents

✨ Features

πŸ” Security & Authentication

  • 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

πŸ‘₯ User Management

  • Multi-role system: Admin, Faculty, Student
  • User registration with profile image upload
  • Secure login/logout with token management
  • Profile management and user data retrieval

πŸ“š Academic Features

  • 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

πŸ› οΈ Technical Features

  • 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

πŸš€ Tech Stack

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

⚑ Quick Start

Prerequisites

  • Node.js 18 or higher
  • MongoDB 6 or higher
  • npm or yarn package manager

1. Clone and Install

git clone <repository-url>
cd campusHub
npm install

2. Environment Setup

cp .env.example .env
# Edit .env with your configuration

3. Start Development Server

# Using Docker for MongoDB (recommended)
docker compose up -d

# Start the application
npm run dev

The server will start on http://localhost:4000

4. Verify Installation

curl http://localhost:4000/api/v1/healthCheck

πŸ“– API Documentation

Base URL

http://localhost:4000/api/v1

Core Endpoints

πŸ” Authentication

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 βœ…

πŸ“’ Announcements

Method Endpoint Description Roles
GET /announcements List announcements All
POST /announcements Create announcement Admin, Faculty

πŸ“š Courses

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

πŸ“Š Attendance

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

🎯 Results

Method Endpoint Description Roles
POST /results Create results Admin
GET /results/:studentId Get student results All

πŸŽ‰ Events

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

πŸ”‘ Authentication

Authentication Flow

  1. Register/Login β†’ Receive JWT in http-only cookie
  2. Generate API Key β†’ Get API key for protected endpoints
  3. 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

πŸ”§ Environment Variables

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

πŸ› οΈ Development

Available Scripts

npm run dev      # Start development server with nodemon
npm start        # Start production server

Database Setup

Option 1: Docker (Recommended)

docker compose up -d

Option 2: Local MongoDB

# Install MongoDB locally
# Update MONGO_URI in .env to: mongodb://localhost:27017

πŸ“ API Response Format

Success Response

{
  "statusCode": 200,
  "data": {
    // Response data
  },
  "message": "Success",
  "success": true
}

Error Response

{
  "statusCode": 400,
  "message": "Validation error",
  "success": false,
  "errors": [
    {
      "field": "email",
      "message": "Invalid email format"
    }
  ]
}

πŸ” Troubleshooting

Common Issues

  1. MongoDB Connection Error

    • Ensure MongoDB is running
    • Check MONGO_URI in .env file
    • Verify network connectivity
  2. JWT Token Issues

    • Check ACCESS_TOKEN_SECRET is set
    • Ensure cookies are enabled in client
    • Verify token expiration
  3. File Upload Problems

    • Check multer configuration
    • Verify public/temp directory exists
    • Ensure file size limits are appropriate

Debug Mode

DEBUG=campusHub:* npm run dev

πŸ“„ License

This project is licensed under the ISC License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Sumit Singh


⭐ Star this repository if you found it helpful!

Made with ❀️

About

A backend Rest apis for a campus portal system where users have different roles (Student, Faculty, Admin). Each role has specific permissions to access and modify resources like announcements, results, events ,courses, enrollment , attendance and course materials.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published