Prerequisites
Before you begin, ensure you have the following installed:- Node.js >= 18.0.0 (Download)
- npm >= 9.0.0 (comes with Node.js)
- MongoDB >= 6.0 (Download)
- Git for cloning the repository
- Gmail account with App Password for email functionality (optional for development)
You can use a hosted MongoDB database like MongoDB Atlas (free tier available) instead of installing MongoDB locally.
Installation steps
Set up the database
Start MongoDB locally or create a cloud database:Option 1: Local MongoDBOption 2: MongoDB Atlas (Cloud)Your connection string should look like:
- Create a free account at MongoDB Atlas
- Create a new cluster (free tier available)
- Create a database user and whitelist your IP
- Get your connection string
MongoDB collections and indexes are automatically created on the first server start. You don’t need to run any migration scripts.
Configure environment variables
The backend requires environment variables for database connection, JWT secrets, and email configuration.Create a Add the following configuration (replace with your actual values):
.env file in the backend directory:backend/.env
Generate JWT secrets
Generate secure random strings for Copy the generated strings into your
JWT_SECRET and JWT_REFRESH_SECRET:.env file.Set up Gmail App Password (optional)
For email verification and password reset functionality, you need a Gmail App Password:
- Go to your Google Account
- Navigate to Security > 2-Step Verification
- Scroll down to App passwords
- Select Mail and generate a new password
- Copy the 16-character password and paste it as
SMTP_PASSin your.envfile
You can skip this step during development. Email sending will fail, but the application will still work for testing other features.
Configure frontend environment (optional)
The frontend uses Vite’s proxy configuration to forward API requests to the backend. No additional environment variables are required.If you need to customize the backend URL, create a
.env file in the frontend directory:frontend/.env
Install dependencies
Install dependencies for both frontend and backend:The backend uses these key dependencies:The frontend uses React 19 with Vite and Tailwind CSS v4:
backend/package.json
frontend/package.json
Start the development servers
Open two terminal windows and start both servers:You should see output similar to:
Backend
Frontend
Project structure
Understanding the project structure will help you navigate the codebase:Available scripts
Run these commands from the root directory:| Script | Description |
|---|---|
npm run dev | Start both frontend and backend concurrently |
npm run dev:backend | Start backend server only |
npm run dev:frontend | Start frontend dev server only |
npm run install:all | Install dependencies for root, backend, and frontend |
backend/ directory):
| Script | Description |
|---|---|
npm run dev | Start backend with nodemon (auto-reload) |
npm start | Start backend in production mode |
frontend/ directory):
| Script | Description |
|---|---|
npm run dev | Start Vite dev server |
npm run build | Build for production |
npm run preview | Preview production build |
Environment variables reference
Backend environment variables
| Variable | Description | Required | Default |
|---|---|---|---|
PORT | Backend server port | No | 5000 |
NODE_ENV | Environment mode | No | development |
MONGODB_URI | MongoDB connection string | Yes | - |
JWT_SECRET | JWT signing secret (min 32 chars) | Yes | - |
JWT_REFRESH_SECRET | Refresh token secret (min 32 chars) | Yes | - |
JWT_EXPIRES_IN | Access token expiry time | No | 1h |
JWT_REFRESH_EXPIRES_IN | Refresh token expiry time | No | 7d |
SMTP_HOST | SMTP server hostname | No | smtp.gmail.com |
SMTP_PORT | SMTP server port | No | 587 |
SMTP_USER | SMTP email address | No | - |
SMTP_PASS | SMTP password/app password | No | - |
FROM_EMAIL | Sender email address | No | - |
FRONTEND_URL | Frontend URL for email links | No | http://localhost:5173 |
UPLOAD_DIR | Directory for uploaded files | No | backend/public/uploads |
Frontend environment variables
| Variable | Description | Required | Default |
|---|---|---|---|
VITE_BACKEND_URL | Backend API URL | No | http://localhost:5000 |
Vite automatically exposes environment variables prefixed with
VITE_ to the client-side code.Troubleshooting
Database connection fails
Error:Connection refused or Authentication failed
Solution:
-
Ensure MongoDB is running:
-
Verify your
MONGODB_URIin.env:- Check connection string format
- Ensure credentials are correct for Atlas
- For local: verify MongoDB is listening on port 27017
-
Test the connection manually:
-
Verify your
DATABASE_URLin.env:- Check username, password, host, and database name
- Ensure the database exists:
psql -l
-
Test the connection manually:
Email not sending
Error:Invalid login or SMTP error
Solution:
- Enable 2-Step Verification on your Google account
- Generate an App Password (not your regular Gmail password)
- Verify
SMTP_USERandSMTP_PASSin.env - Check that
SMTP_PORTis587(not465)
Frontend can’t reach backend
Error:Network Error or ERR_CONNECTION_REFUSED
Solution:
-
Ensure backend is running on port 5000:
-
Check Vite proxy configuration in
frontend/vite.config.js: - Clear browser cache and restart the frontend dev server
Port already in use
Error:EADDRINUSE: address already in use :::5000
Solution:
-
Find and kill the process using the port:
-
Or change the port in
backend/.env:
JWT token errors
Error:jwt malformed or invalid signature
Solution:
- Ensure
JWT_SECRETandJWT_REFRESH_SECRETare properly set in.env - Clear browser cookies and local storage
- Log out and log back in to get a fresh token
Module not found errors
Error:Cannot find module 'express' or similar
Solution:
-
Delete
node_modulesand reinstall: -
Verify Node.js version:
Next steps
Quickstart
Create an account and place your first order
API Reference
Explore all available API endpoints
Authentication
Learn about JWT authentication flow
Deployment
Deploy CampusBite to production