This is the backend API for a flashcard learning web application. It provides endpoints for user authentication, flashcard set management, topic handling, and AI-assisted flashcard generation. It is part of the codebase for PERN stack, utilising PostgreSQL through Sequelize ORM, Passport for authentication with JWT, and OpenAI API.
- User `authentication (register, login, logout)
- Flashcard set CRUD operations
- Topic management
- AI-assisted flashcard generation
- Protected and public routes
- Node.js
- Express.js
- Passport.js for authentication
- PostgreSQL
- Sequelize ORM
API Routes
POST /api/auth/register: Register a new userPOST /api/auth/login: User loginPOST /api/auth/logout: User logout
GET /api/ai/flashcard-sets/:id: Get a specific flashcard setGET /api/ai/dashboard/flashcard-sets: Get user's flashcard setsPOST /api/ai/flashcard-sets: Create a new flashcard set with flashcardsPUT /api/ai/flashcard-sets/:id/update: Update a flashcard setDELETE /api/ai/flashcard-sets/:id: Delete a flashcard set
POST /api/ai/topics: Create a new topicGET /api/ai/topics/:id: Get a specific topicGET /api/ai/topics: Get all topicsGET /api/ai/topics-dashboard/:id: Get topic dashboardDELETE /api/ai/topics/:id: Delete a topic
POST /api/ai/flashcards: Generate new flashcards using AIGET /api/ai/flashcards/:id: Get a specific flashcard
- Clone the repository
- Install dependencies:
npm install - Set up environment variables in a
.envfile - Initialize the database
- Start the server:
npm start
Environment Variables
Most of the default values assued in ./src/constants/index.js
-
PROJECT_NAME: Name branding for the project -
NODE_ENV: Set to 'production' for production environment -
SERVER_PORT: Port number for the server -
CLIENT_URL: URL of the frontend client for CORS -
DB_ADDRESS: Address to the database -
DB_PORT: Port to the database -
DB_SUPERUSER: Superuser for database -
DB_PASSWORD: Password for database -
DB_URL: URL for the database, when deploying - Overwrites local settings when present -
JWT_SECRET: Secret for JWT -
COOKIE_DOMAIN: For JWT Security, when domain is not on public suffix list. -
OPENAI_API_KEYY: API KEY to OpenAI(Spelling due to overriding with local key.) -
OPENAI_API_URL: URL for OPENAI if different or changed
The API uses JSON Web Tokens (JWT) for authentication. Protected routes require a valid JWT to be included in the Authorization header of the request.
The API includes centralized error handling middleware to process and return appropriate error responses.
The API includes routes for AI-assisted flashcard generation. The specific AI model and integration details are implemented in the ./src/controllers/ai.js.