This guide walks you through setting up and running the backend API on your local machine.
Before you begin, ensure you have the following installed:
- Node.js v18 or higher (Download)
- Bash terminal (Git Bash on Windows, native terminal on Linux/macOS)
- MongoDB (optional, for production use) - See Database Setup Guide
The start.sh script automatically handles dependency installation, environment setup, and server startup.
git clone https://github.com/fraidakis/software-engineering-2-backend.git
cd software-engineering-2-backendRun the start script in a Bash terminal:
bash start.shThe script will:
- ✅ Install dependencies (
npm install) - ✅ Create
.envfile from.env.exampleif it doesn't exist - ✅ Start the server in development mode with auto-reload (nodemon) if
NODE_ENV=development - ✅ Start the server in production mode otherwise
Once the server is running, you can access:
- API Base URL:
http://localhost:3001 - API Documentation:
http://localhost:3001/api-docs - Health Check:
http://localhost:3001/health
If you prefer manual setup or need more control:
npm installCreate a .env file in the root directory:
cp .env.example .envEdit .env with your configuration:
# Server Configuration
PORT=3001
# Security - IMPORTANT: Change in production!
JWT_SECRET=your_strong_secret_here_minimum_32_chars
# Environment
NODE_ENV=development
# Database Configuration
MONGODB_URI=mongodb+srv://username:password@host/database?retryWrites=true&w=majority
USE_MONGODB=false # Set to true to use MongoDB, false for in-memory database
# CORS Configuration
CORS_ORIGIN=http://localhost:3000 # Frontend URL(s), comma-separatedDevelopment mode (with auto-reload):
npm run devProduction mode:
npm startThe API supports two database modes. Choose the one that fits your needs:
Recommended for: Development, testing, and quick demos
Configuration:
USE_MONGODB=falseFeatures:
- ✅ No MongoDB installation required
- ✅ Pre-loaded with sample data
- ✅ Fast startup
- ✅ Perfect for testing
⚠️ Data resets on server restart
Sample Data Includes:
- 10+ places with reviews and ratings
- 3 test users with different roles
- Sample preferences and favorites
Recommended for: Production, persistent data
Configuration:
USE_MONGODB=true
MONGODB_URI=mongodb+srv://username:password@host/databaseSetup Steps:
- Create a MongoDB database (local or cloud)
- Get your connection string
- Update
MONGODB_URIin.env - Set
USE_MONGODB=true
📖 For detailed MongoDB setup instructions, see: Database Setup Guide
| Variable | Description | Default | Required |
|---|---|---|---|
PORT |
Server port number | 3001 |
No |
JWT_SECRET |
Secret key for JWT token signing | - | Yes |
NODE_ENV |
Environment mode (development or production) |
production |
No |
USE_MONGODB |
Use MongoDB (true) or in-memory database (false) |
false |
No |
MONGODB_URI |
MongoDB connection string | - | Only if USE_MONGODB=true |
CORS_ORIGIN |
Allowed CORS origins (comma-separated) | http://localhost:3000 |
No |
Optimized for development with auto-reload:
NODE_ENV=development
npm run devFeatures:
- Auto-reload on code changes (nodemon)
- Detailed error messages
- Request/response logging
Optimized for production performance:
NODE_ENV=production
npm startFeatures:
- No auto-reload
- Minimal logging
- Optimized performance
curl http://localhost:3001/healthExpected Response:
{
"status": "healthy",
"timestamp": "2025-12-05T10:30:00.000Z"
}Open your browser and navigate to:
http://localhost:3001/api-docs
You should see the interactive Swagger UI documentation.
Login with test user:
curl -X POST http://localhost:3001/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "john.doe@example.com", "password": "password123"}'Expected Response:
{
"success": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"email": "john.doe@example.com",
"name": "John Doe"
}
}
}npm testnpm run test:watchnpm run test:coverageCoverage reports are generated in the coverage/ directory.
📖 For detailed testing documentation, see: ../tests/README.md
Error: EADDRINUSE: address already in use :::3001
Solution: Change the port in .env:
PORT=3002Error: JWT_SECRET is not defined
Solution: Add JWT_SECRET to your .env file:
JWT_SECRET=your_strong_secret_here_minimum_32_charsError: MongoServerError: Authentication failed
Solution:
- Verify your
MONGODB_URIis correct - Check username and password
- Ensure your IP is whitelisted (for cloud databases)
- Or switch to in-memory database:
USE_MONGODB=false
Error: Access to XMLHttpRequest has been blocked by CORS policy
Solution: Update CORS_ORIGIN in .env with your frontend URL:
CORS_ORIGIN=http://localhost:3000For multiple origins:
CORS_ORIGIN=http://localhost:3000,http://localhost:3001Now that your backend is running:
- 📖 Explore the API - Learn about available endpoints
- 🏗️ Understand the Architecture - Deep dive into design patterns
- 🔌 Connect the Frontend - Set up the React frontend
- Database Setup: See Database Setup Guide
- API Reference: See API Overview or Swagger UI
- Testing Guide: See ../tests/README.md
- CI/CD Documentation: See ../.github/workflows/README.md