A comprehensive RESTful API for managing course registration, scheduling, and academic administration built with NestJS and MongoDB.
- Features
- Tech Stack
- Project Structure
- How to Run the Project (Step by Step)
- Environment Variables
- API Documentation
- Modules
- Authentication
- Database Schema
- API Endpoints
- User Management: Support for multiple user roles (Admin, Student, Professor)
- Authentication & Authorization: JWT-based authentication with role-based access control
- Course Management: Create and manage lessons, prerequisites, and course units
- Classroom Management: Manage classrooms with capacity and faculty associations
- Section Scheduling: Create sections with time schedules and conflict detection
- Student Enrollment: Manage student registrations and major assignments
- Faculty Management: Handle professor profiles and faculty associations
- Swagger Documentation: Interactive API documentation
- Data Validation: Comprehensive input validation using class-validator
- MongoDB Integration: Efficient data storage with Mongoose ODM
- Framework: NestJS - Progressive Node.js framework
- Language: TypeScript
- Database: MongoDB with Mongoose
- Authentication: JWT (JSON Web Tokens) with Passport
- Validation: class-validator, class-transformer
- Documentation: Swagger/OpenAPI
- Security: bcrypt for password hashing
src/
βββ admin/ # Admin management module
βββ auth/ # Authentication & authorization
βββ classroom/ # Classroom management
βββ faculty/ # Faculty management
βββ lesson/ # Course/lesson management
βββ major/ # Major/degree program management
βββ professor/ # Professor management
βββ section/ # Section scheduling and enrollment
βββ student/ # Student management
βββ user/ # User management
βββ seed/ # Database seeding utilities
βββ utils/ # Utility functions and enums
This guide walks you through setting up and running the CRS API from scratch.
You need Node.js v20.x or higher (includes npm).
# Using NodeSource (recommended for v20)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify
node --version # Should show v20.x.x
npm --version # Should show 10.x.x or higher# Using Homebrew (install from https://brew.sh if needed)
brew install node@20
# Or use nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Restart terminal, then:
nvm install 20
nvm use 20
# Verify
node --version
npm --version- Download the LTS installer from nodejs.org (v20 or higher).
- Run the installer and follow the steps (ensure "Add to PATH" is checked).
- Open a new Command Prompt or PowerShell and run:
node --version
npm --versionThe API uses MongoDB as its database. You can run it locally or use MongoDB Atlas (cloud).
# Import MongoDB public key and add repository
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Install MongoDB
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod # Start on boot
# Verify (optional)
mongosh --eval "db.runCommand({ ping: 1 })"# Using Homebrew
brew tap mongodb/brew
brew install mongodb-community@7.0
# Start MongoDB (run in background)
brew services start mongodb-community@7.0
# Verify (optional)
mongosh --eval "db.runCommand({ ping: 1 })"- Download the MongoDB Community Server installer from MongoDB Download Center.
- Run the installer and choose "Complete". Optionally install MongoDB Compass (GUI).
- MongoDB is usually installed at
C:\Program Files\MongoDB\Server\7.0\bin. Add it to your PATH or run from that folder:- Start the server:
mongod --dbpath C:\data\db(createC:\data\dbif needed).
- Start the server:
- Go to MongoDB Atlas and create a free account.
- Create a free cluster and get your connection string (e.g.
mongodb+srv://user:pass@cluster.mongodb.net/crs). - You will use this URL in the
.envfile in Step 5 instead ofmongodb://localhost:27017/crs.
Before continuing, ensure everything is installed:
node --version # v20.x.x or higher
npm --version # 10.x.x or higher
mongosh --version # 2.x.x (optional; not needed if using Atlas only)If using local MongoDB, ensure the service is running (sudo systemctl status mongod on Linux, or check Services on Windows).
git clone <repository-url>
cd crs-apiReplace <repository-url> with your actual repo URL (e.g. https://github.com/your-username/crs-api.git).
Where to run: Inside the project folder (the crs-api directoryβthe same folder that contains package.json). If you just cloned and ran cd crs-api, you are already there.
# Make sure you're in the project root (you should see package.json here)
# pwd should show something like: /path/to/crs-api
npm install
# or
npm iThis installs NestJS, Mongoose, and all other dependencies from package.json.
Create a .env file in the root of the project (same folder as package.json):
# Linux/macOS
touch .env
# Then open .env in your editor and add:Add the following (adjust if you use Atlas or different port):
MONGO_URI_CONN=mongodb://localhost:27017/crs
PORT=3001
JWT_SECRET=your-secret-key-here
JWT_EXPIRES_IN=7d- Local MongoDB: Use
mongodb://localhost:27017/crs. - MongoDB Atlas: Use your Atlas connection string (e.g.
MONGO_URI_CONN=mongodb+srv://user:password@cluster.mongodb.net/crs). - JWT_SECRET: Replace
your-secret-key-herewith a long, random string for production.
Development mode (with hot-reload):
npm run start:devYou should see something like:
[Nest] ... LOG [NestApplication] Nest application successfully started
[Nest] ... LOG Application is running on: http://localhost:3001
Production mode (build then run):
npm run build
npm run start:prod-
Health check: Open in browser or with curl:
http://localhost:3001(or the port you set in.env).
-
Swagger API docs:
-
Next: Run the seed (Step 9) so you can log in. Default credentials are username:
admin, password:admin.
You need to run the seed after the project is running to create the initial admin user. Without this, you cannot log in.
curl -X POST http://localhost:3001/seed/admin-userOr use the same endpoint from the Swagger UI at http://localhost:3001/api/docs.
Default admin login credentials:
| Field | Value |
|---|---|
| Username | admin |
| Password | admin |
Use these to log in at POST /auth/login or via Swagger.
| Step | Action | Command / Action |
|---|---|---|
| 1 | Install Node.js (v20+) | See Step 1 for your OS |
| 2 | Install MongoDB | See Step 2 (local or Atlas) |
| 3 | Verify | node -v, npm -v |
| 4 | Clone repo | git clone <url> && cd crs-api |
| 5 | Install dependencies (in crs-api folder) |
npm install or npm i |
| 6 | Create .env |
Copy env vars from Step 6 |
| 7 | Run app | npm run start:dev |
| 8 | Open docs | http://localhost:3001/api/docs |
| 9 | Run seed (required) | POST http://localhost:3001/seed/admin-user β then login with admin / admin |
- "Cannot connect to MongoDB": Ensure MongoDB is running locally, or that your Atlas URL, username, and password in
.envare correct and the IP is allowed in Atlas. - Port already in use: Change
PORTin.env(e.g. to3001). - Module not found: Run
npm installagain from the project root. - Permission denied (Linux): Use
sudoonly where indicated (e.g. for installing Node.js or MongoDB), not fornpm installor running the app.
Create a .env file in the root directory:
MONGO_URI_CONN=mongodb://localhost:27017/crs
PORT=3001
JWT_SECRET=your-secret-key-here
JWT_EXPIRES_IN=7dOnce the server is running, access the Swagger documentation at:
http://localhost:3001/api/docs
The API documentation provides:
- Interactive API explorer
- Request/response schemas
- Authentication testing
- Example payloads
- User registration and management
- Role-based user profiles (Admin, Student, Professor)
- User authentication
- JWT token generation and validation
- Login/logout functionality
- Role-based access control (RBAC)
- Protected routes with guards
- Course/lesson creation and management
- Prerequisite management
- Lesson types (General, Specialized, Mandatory, Elective, Lab)
- Unit management
- Section creation with scheduling
- Time slot conflict detection
- Classroom assignment
- Professor assignment
- Student enrollment management
- Classroom management
- Capacity tracking
- Faculty association
- Room number validation
- Student profile management
- Major assignment
- Student ID generation
- Professor profile management
- Education credentials
- Faculty association
- Faculty/department management
- Academic major/degree program management
The API uses JWT (JSON Web Tokens) for authentication:
-
Login: POST
/auth/login- Returns JWT token and user information
-
Protected Routes: Include token in Authorization header
Authorization: Bearer <your-jwt-token> -
Roles:
ADMIN: Full system accessPROFESSOR: Professor-specific operationsSTUDENT: Student-specific operations
- User: Base user information with role-based profiles
- Student: Student-specific data with major association
- Professor: Professor data with faculty and education info
- Admin: Administrative user data
- Lesson: Course information with prerequisites
- Section: Class sections with schedules and enrollments
- Classroom: Room information with capacity
- Faculty: Academic faculty/department
- Major: Academic major programs
- User β Student/Professor/Admin (one-to-one)
- Section β Professor, Classroom, Lesson (many-to-one)
- Section β Students (many-to-many)
- Student β Major (many-to-one)
- Professor β Faculty (many-to-one)
- Classroom β Faculty (many-to-one)
POST /auth/login- User login
GET /lesson-admin- Get all lessonsGET /lesson-admin/:id- Get lesson by IDPOST /lesson-admin- Create lesson (Admin only)PUT /lesson-admin/:id- Update lesson (Admin only)DELETE /lesson-admin/:id- Delete lesson (Admin only)
GET /section- Get all sectionsGET /section/:id- Get section by IDPOST /section- Create section (Admin only)PUT /section/:id- Update section (Admin only)DELETE /section/:id- Delete section (Admin only)
GET /classroom- Get all classroomsGET /classroom/:id- Get classroom by IDPOST /classroom- Create classroom (Admin only)PUT /classroom/:id- Update classroom (Admin only)DELETE /classroom/:id- Delete classroom (Admin only)
GET /student- Get all studentsPOST /student- Create student
GET /professor- Get all professorsPOST /professor- Create professor
GET /faculty- Get all facultiesPOST /faculty- Create faculty
GET /major- Get all majorsPOST /major- Create major
- Automatic conflict detection for classroom time slots
- Support for multiple schedules per section
- Time format validation (HH:mm, 24-hour format)
- Day-of-week enum validation
- Comprehensive input validation
- Regex patterns for time formats
- Enum validation for roles and types
- MongoDB ObjectId validation
- Password hashing with bcrypt
- JWT token-based authentication
- Role-based access control
- CORS configuration
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov# Development
npm run start:dev # Start in watch mode
# Production
npm run build # Build for production
npm run start:prod # Start production server
# Code quality
npm run lint # Run ESLint
npm run format # Format code with PrettierContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the UNLICENSED License.
Matin Sadeghi
For more information, visit the API Documentation when the server is running.