Hệ thống quản lý trường học CTECH - Full-stack web application với Node.js + Express backend và Vanilla JavaScript frontend.
Trước khi bắt đầu, đảm bảo máy tính đã cài đặt:
# Nếu dùng Git
git clone https://github.com/GauCandy/Ctech-Web
cd "Api Web"
# Hoặc download ZIP và giải nénnpm installLệnh này sẽ cài đặt tất cả packages cần thiết:
- express, mysql2, multer
- openai (cho chatbot)
- pdfjs-dist (parse PDF thời khóa biểu)
- dotenv, cors, và các dependencies khác
Mở MySQL Command Line hoặc MySQL Workbench và chạy:
CREATE DATABASE ctech_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Tạo file .env ở thư mục gốc với nội dung:
# Database Configuration
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_mysql_password
DB_NAME=ctech_db
# Server Configuration
PORT=3000
# Session Configuration (optional, có giá trị mặc định)
SESSION_TIMEOUT=3600 # 1 giờ (3600 giây)
SESSION_REMEMBER_TIMEOUT=2592000 # 30 ngày (2592000 giây)
# OpenAI Configuration (cho chatbot)
OPENAI_API_KEY=sk-your-openai-api-key
# Bank Information (cho VietQR payment)
BANK_BIN=970422
BANK_NUMBER=0372360619
BANK_OWNER=Tran Tuan Tu
BANK_NAME=MB BankLưu ý:
- Thay
your_mysql_passwordbằng mật khẩu MySQL của bạn - Nếu không dùng chatbot, có thể bỏ qua
OPENAI_API_KEY - Bank info dùng để tạo VietQR code thanh toán
mkdir uploads
mkdir uploads\servicesHoặc trên Linux/Mac:
mkdir -p uploads/servicesLưu ý: Database schema sẽ được tự động tạo khi chạy server lần đầu tiên. Server tự động đọc file src/backend/database/schema.sql và tạo các bảng:
user_accounts- Tài khoản người dùngstudents,teachers,admin_profiles- Hồ sơ theo roleuser_sessions- Session tokensservices- Dịch vụ trường cung cấporders- Đơn hàngvouchers- Mã giảm giástudent_device_registry- Quản lý thiết bị sinh viênstudent_device_logins- Lịch sử đăng nhập thiết bị
Không cần chạy manual SQL script!
node index.jsNếu thành công, bạn sẽ thấy:
Database connection pool is ready.
Services catalog exported to ... (X services, extraTxt=Y).
API server listening on port 3000
Lưu ý: Lần chạy đầu tiên, server sẽ tự động:
- Tạo tất cả các bảng từ schema.sql
- Export services catalog cho chatbot
- Khởi động web server trên port 3000
Mở trình duyệt và truy cập:
- Trang chủ: http://localhost:3000
- Đăng nhập: http://localhost:3000/login
- Dịch vụ: http://localhost:3000/services
- Lịch học: http://localhost:3000/schedule
- Admin: http://localhost:3000/admin
curl http://localhost:3000/api/statusHoặc mở: http://localhost:3000/api/status
Sau khi setup xong, hệ thống sẽ tự động tạo tài khoản admin từ environment variable. Kiểm tra trong code src/backend/api/features/auth/services/adminAccountService.js để biết username/password.
Tạo tài khoản sinh viên mới:
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"fullName": "Nguyen Van A",
"email": "student@example.com",
"phoneNumber": "0123456789",
"classCode": "CNTT2024A",
"department": "Công nghệ thông tin",
"password": "password123"
}'Api Web/
├── src/
│ ├── backend/ # Backend API
│ │ ├── api/ # API routes và controllers
│ │ ├── database/ # Database setup và connection
│ │ └── server/ # Express app configuration
│ └── frontend/ # Frontend static files
│ ├── css/ # Stylesheets
│ ├── js/ # JavaScript modules
│ ├── img/ # Images
│ └── *.html # HTML pages
├── uploads/ # Uploaded files
├── index.js # Application entry point
├── package.json # Dependencies
└── .env # Environment configuration
# Khởi động server
node index.js
# Hoặc dùng nodemon để auto-restart khi code thay đổi
npm install -g nodemon
nodemon index.jsLogin:
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"SV001","password":"password123"}'Get services:
curl http://localhost:3000/api/servicesGet services với authentication:
curl http://localhost:3000/api/services \
-H "Authorization: Bearer YOUR_TOKEN_HERE"- API.md - Complete API documentation
- src/backend/README.md - Backend architecture
- src/frontend/README.md - Frontend architecture
- ✅ Authentication - Login với session tokens (remember me support)
- ✅ User Management - Quản lý sinh viên, giáo viên, admin
- ✅ Services - Catalog dịch vụ với categories (real-time updates)
- ✅ Orders - Đặt hàng và thanh toán qua VietQR
- ✅ Vouchers - Hệ thống mã giảm giá
- ✅ Timetable - Upload và parse PDF thời khóa biểu
- ✅ Chatbot - AI chatbot với OpenAI (cached responses)
- ✅ Admin Panel - Quản lý users, services, orders
- ✅ Presentation Mode - Thuyết trình với điều khiển từ xa
- ✅ Theme System - Đổi theme động
- ✅ Device Management - 1 thiết bị per sinh viên
Nguyên nhân: Database chưa chạy hoặc thông tin kết nối sai
Giải pháp:
- Kiểm tra MySQL đang chạy:
mysql -u root -p - Kiểm tra thông tin trong
.env(DB_HOST, DB_USER, DB_PASSWORD) - Kiểm tra database
ctech_dbđã được tạo chưa
Nguyên nhân: Đã có process khác đang dùng port 3000
Giải pháp:
- Đổi PORT trong
.env(ví dụ:PORT=3001) - Hoặc kill process đang dùng port 3000:
- Windows:
netstat -ano | findstr :3000rồitaskkill /PID <PID> /F - Linux/Mac:
lsof -ti:3000 | xargs kill
- Windows:
Nguyên nhân: Thiếu API key để dùng chatbot
Giải pháp:
- Nếu không dùng chatbot: Bỏ qua lỗi này, các tính năng khác vẫn hoạt động
- Nếu muốn dùng chatbot: Lấy API key tại https://platform.openai.com/api-keys và thêm vào
.env
Nguyên nhân: Dependencies chưa được cài đặt
Giải pháp:
npm installNguyên nhân: File schema.sql không tồn tại hoặc lỗi quyền
Giải pháp:
- Kiểm tra file
src/backend/database/schema.sqlcó tồn tại không - Server tự động load schema khi khởi động, kiểm tra logs
- Nếu vẫn lỗi, chạy manual:
mysql -u root -p ctech < src/backend/database/schema.sql - Kiểm tra user MySQL có quyền CREATE TABLE không
Quan trọng cho production:
- Đổi tất cả passwords mặc định
- Dùng HTTPS thay vì HTTP
- Đổi password hashing từ SHA256 sang bcrypt:
npm install bcrypt
- Enable CORS properly cho production domain
- Giới hạn upload file size hợp lý
- Dùng Redis thay cho in-memory cache
- Enable rate limiting
- Backup database thường xuyên
| Variable | Required | Default | Description |
|---|---|---|---|
DB_HOST |
✅ | - | MySQL host |
DB_USER |
✅ | - | MySQL username |
DB_PASSWORD |
✅ | - | MySQL password |
DB_NAME |
✅ | - | Database name |
PORT |
❌ | 3000 | Server port |
SESSION_TIMEOUT |
❌ | 3600 | Session TTL (seconds) |
SESSION_REMEMBER_TIMEOUT |
❌ | 2592000 | Remember session TTL (seconds) |
OPENAI_API_KEY |
❌ | - | OpenAI API key (cho chatbot) |
BANK_BIN |
❌ | - | Bank BIN cho VietQR |
BANK_NUMBER |
❌ | - | Bank account number |
BANK_OWNER |
❌ | - | Bank account owner |
BANK_NAME |
❌ | - | Bank name |
- Chrome/Edge >= 90
- Firefox >= 88
- Safari >= 14
- Mobile browsers (iOS Safari, Chrome Android)
Nếu gặp vấn đề:
- Kiểm tra Troubleshooting section
- Xem logs trong console khi chạy
node index.js - Kiểm tra API.md để hiểu cách API hoạt động
- Kiểm tra MySQL logs nếu có lỗi database
Proprietary - CTECH School Management System
Quick Start Summary:
# 1. Cài dependencies
npm install
# 2. Tạo database
mysql -u root -p
CREATE DATABASE ctech_db;
# 3. Tạo file .env với DB credentials
# (xem mẫu ở trên)
# 4. Chạy server
node index.js
# 5. Mở trình duyệt
# http://localhost:3000Tới đây là hết rồi bạn đã có thể thiết lập thành công demo! 🎉