@studious-lms/server
v1.2.47
Published
Backend server for Studious application
Readme
Studious Backend Server
This is the backend server for the Studious application. It provides a RESTful and real-time API for managing users, classes, assignments, attendance, and more, supporting both HTTP (via tRPC) and WebSocket (via Socket.IO) communication.
Features
- Express.js server with CORS support
- tRPC for type-safe API endpoints
- Socket.IO for real-time features
- Prisma ORM with PostgreSQL for database management
- User authentication and session management
- Class, assignment, attendance, and file management
- Structured logging for requests and server events
Getting Started
Prerequisites
- Node.js (v18+ recommended)
- npm (v9+ recommended)
- PostgreSQL database
Installation
Clone the repository:
git clone <repo-url> cd serverInstall dependencies:
npm installSet up environment variables: Copy the example environment file and update with your values:
cp .env.example .envThen edit
.envwith your configuration. See.env.examplefor all required variables.Set up the database:
- Create a PostgreSQL database named
easy_lms - Run the Prisma migrations:
npx prisma migrate dev --name init - Generate the Prisma client:
npx prisma generate
- Create a PostgreSQL database named
Running the Server
Development mode (with hot reload):
npm run devProduction build:
npm run build npm start
The server will start on the port specified in your .env file (default: 3001).
API Overview
- tRPC endpoints: Available at
/trpc - WebSocket: Available at
/socket.io/ - CORS: Configured to allow requests from the frontend app URL via
NEXT_PUBLIC_APP_URL
Project Structure
src/
index.ts # Main server entry point
routers/ # tRPC routers for API endpoints
socket/ # Socket.IO event handlers
middleware/ # Express and tRPC middleware
utils/ # Utility functions (e.g., logger)
lib/ # Shared libraries
types/ # TypeScript types
prisma/
schema.prisma # Prisma database schema
migrations/ # Database migrationsDatabase
- Database: PostgreSQL
- ORM: Prisma
- Schema: Defined in
prisma/schema.prisma - Migrations: Stored in
prisma/migrations/
Database Commands
# Generate Prisma client
npx prisma generate
# Run migrations
npx prisma migrate dev
# Reset database
npx prisma migrate reset
# Open Prisma Studio (database GUI)
npx prisma studioScripts
npm run dev— Start in development mode with hot reloadnpm run build— Compile TypeScript to JavaScriptnpm start— Start the compiled servernpm test— Run tests with Vitestnpm run test:run— Run tests oncenpm run test:watch— Run tests in watch modenpm run test:coverage— Run tests with coverage report
Environment Variables
| Variable | Description | Default |
|----------|-------------|---------|
| DATABASE_URL | PostgreSQL connection string | Required |
| PORT | Server port | 3001 |
| NEXT_PUBLIC_APP_URL | Frontend app URL for CORS | http://localhost:3000 |
| NODE_ENV | Environment mode | development |
| LOG_MODE | Logging level | info |
Development
The server uses TypeScript and includes:
- tRPC for type-safe API development
- Socket.IO for real-time communication
- Prisma for database operations
- Express middleware for CORS and logging
Testing
Tests use a separate environment configuration:
Set up test environment:
cp .env.test.example .env.testUpdate
.env.testwith your test database configuration.Create test database:
createdb easy_lms_testRun tests:
npm test
The test environment:
- Uses a separate database (
easy_lms_test) - Runs on a different port (3002)
- Has minimal logging (error level only)
- Mocks external services (Google Cloud, Pusher, etc.)
