This file documents our step-by-step journey building the backend for this project. It’s written so “future me” can come back and re-learn the concepts we used.
- Frontend (Vite + React + TS) is running and already “backend-ready”.
- Frontend calls these endpoints (no
/apiprefix):POST /auth/registerPOST /auth/loginGET/POST/PUT/DELETE /notesGET/POST/PUT/DELETE /projects
- We will implement a backend that supports both:
- the frontend paths above, and
- the Backend PRD paths (with
/api/...) for compatibility.
From PRD.md and BackendPRD.md:
- Node.js + Express
- MongoDB (Atlas Free Tier) + Mongoose
- JWT auth (access token)
- bcrypt password hashing
- Clean architecture: routes → controllers → services → models
- Protected routes for user-specific data isolation
We’ll create:
backend/
src/
server.ts
app.ts
config/db.ts
controllers/
middleware/
models/
routes/
utils/
types/
package.json
tsconfig.json
.env.example
Why TypeScript?
- The frontend already uses TS.
- TS catches common backend mistakes early (wrong types, missing fields).
Request → middleware → route handler → controller → service → DB → response.
JWT is a signed token that proves “this request is from user X”. We’ll:
- Sign a token at login/register.
- Verify it on protected routes.
- Put it in
Authorization: Bearer <token>.
Mongoose gives:
- schemas (shape + validation)
- models (query API)
- document lifecycle hooks
Frontend dev server runs at http://localhost:8080.
Backend will run at http://localhost:3000.
CORS allows the browser to call the backend from a different origin.
We will:
- Add
backend/package.jsonand install dependencies - Create
server.tsandapp.ts - Add Mongo connection
- Add routes for auth/notes/projects
- Run backend locally
Why: no local install, works everywhere.
-
Create an account at MongoDB Atlas and create a free tier cluster.
-
Create a database user (username + password).
-
Network Access → allow your IP:
- easiest during dev: add
0.0.0.0/0(allows all). You can tighten later.
- Get your connection string:
- Go to Cluster → Connect → Drivers → copy the URI.
- Put it into
backend/.envasMONGODB_URI.
Example:
MONGODB_URI=mongodb+srv://<user>:<pass>@<cluster>/<db>?retryWrites=true&w=majority
- Install MongoDB Community Server.
- Ensure the Mongo service is running.
- Use:
MONGODB_URI=mongodb://127.0.0.1:27017/ai-knowledge-hub
From repo root:
cd backend
npm install
npm run dev
Once backend is running, we can switch the frontend to real API mode:
- Create
ai-knowledge-hub/.env.local(do not commit secrets)
VITE_USE_MOCK_API=false
VITE_API_BASE_URL=http://localhost:3000
- Restart the frontend dev server.
- If Mongo connection fails:
- verify
MONGODB_URIinbackend/.env - ensure your IP is allowed in MongoDB Atlas Network Access
- verify
- If requests fail in browser:
- check CORS origin
- check backend logs
Now that the backend is running at http://127.0.0.1:3000, we can switch the frontend from mock mode to real API mode.
We do this by creating .env.local (Vite reads it automatically):
VITE_USE_MOCK_API=false
VITE_API_BASE_URL=http://127.0.0.1:3000
What this changes:
VITE_USE_MOCK_API=false→ frontend stops usinglocalStoragemock DB.VITE_API_BASE_URL=...→ frontendfetchcalls go to the Express server.
Expected behavior after switching:
- Login/Register should create real users in MongoDB.
- Notes/Projects pages should load from the backend (empty at first for new users).