- Node.js 16+
- PostgreSQL 14+
- npm/yarn
cd backend
npm install
cp .env.example .env # Configure your environment variables
npm run dev- Development:
http://localhost:5000/api - Swagger UI:
http://localhost:5000/api-docs
POST /api/auth/signup
Content-Type: application/json
{
"username": "john_doe",
"email": "john@example.com",
"password": "password123"
}POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}
# Response
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}GET /api/issuesPOST /api/issues
Content-Type: multipart/form-data
Authorization: Bearer YOUR_JWT_TOKEN
{
"title": "Pothole on Main Street",
"description": "Large pothole causing traffic issues",
"location": "Main Street & 5th Ave",
"category": "road",
"file": [uploaded_image.jpg]
}PATCH /api/issues/:id/status
Content-Type: application/json
Authorization: Bearer ADMIN_JWT_TOKEN
{
"status": "in-progress"
}- JWT Authentication: Secure token-based auth
- Role-based Access: Admin/User permissions
- Rate Limiting: 100 requests per 15 minutes
- Input Validation: Express-validator middleware
- File Upload Security: Multer with file type validation
- CORS Protection: Configured for frontend domain
- Helmet.js: Security headers
- XSS Protection: Clean user inputs
backend/
βββ config/
β βββ db.js # Database connection
β βββ swagger.js # API documentation config
βββ controllers/
β βββ auth.js # Authentication logic
β βββ issues.js # Issues CRUD operations
βββ middlewares/
β βββ validate.js # JWT & validation middleware
β βββ upload.js # File upload handling
β βββ errorHandler.js # Global error handling
βββ models/
β βββ userModel.js # User database operations
β βββ issues.js # Issues database operations
βββ routes/
β βββ auth.js # Authentication routes
β βββ issues.js # Issues routes
βββ uploads/ # File storage directory
βββ server.js # Express app entry point
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(20) DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE issues (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
location VARCHAR(255) NOT NULL,
category VARCHAR(50),
status VARCHAR(20) DEFAULT 'pending',
image_url VARCHAR(500),
user_id INTEGER REFERENCES users(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);# Server
PORT=5000
NODE_ENV=development
# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=civix_db
DB_USER=your_username
DB_PASSWORD=your_password
# JWT
JWT_SECRET=your_super_secret_key
JWT_EXPIRES_IN=24h
# Admin Domain
DOMAIN_NAME=@admin.civix.com
# Email (if implemented)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password# Register
curl -X POST http://localhost:5000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:5000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123"}'# Get all issues
curl -X GET http://localhost:5000/api/issues
# Create issue with file
curl -X POST http://localhost:5000/api/issues \
-H "Authorization: Bearer YOUR_TOKEN" \
-F "title=Test Issue" \
-F "description=Test Description" \
-F "location=Test Location" \
-F "category=road" \
-F "file=@/path/to/image.jpg"{
"success": true,
"data": {
"id": 1,
"title": "Pothole on Main Street",
"status": "pending",
"created_at": "2024-01-15T10:30:00Z"
}
}{
"success": false,
"message": "Validation failed",
"errors": [
{
"field": "email",
"message": "Invalid email format"
}
]
}200- Success201- Created400- Bad Request401- Unauthorized403- Forbidden404- Not Found429- Too Many Requests500- Internal Server Error
# Start development server
npm run dev
# Run with different environments
NODE_ENV=production npm start
# Check API documentation
open http://localhost:5000/api-docs- Rate Limiting: Prevents API abuse
- File Upload Optimization: Efficient image handling
- Database Indexing: Optimized queries
- Error Handling: Comprehensive error responses
- Logging: Request/response logging (if implemented)
GET /api/health
# Returns server status and database connection- Development: Console output
- Production:
logs/directory (if configured)
- Set production environment variables
- Configure database connection
- Set up file storage (local/cloud)
- Configure CORS for production domain
FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 5000
CMD ["npm", "start"]- Fork the repository
- Create feature branch:
git checkout -b feature/new-endpoint - Make changes and test
- Submit pull request
- API Issues: Check
/api-docsfor detailed documentation - Database Issues: Verify connection strings and permissions
- Authentication Issues: Ensure JWT_SECRET is set correctly
- Backend Testing: Jest, Supertest, MongoDB Memory Server
- Frontend Unit/Component Testing: Vitest, React Testing Library
- Frontend E2E Testing: Cypress
We have a comprehensive testing suite to ensure code quality and stability.
These tests cover the API endpoints. They run against an in-memory MongoDB database to ensure a clean, isolated environment for each test run, preventing any impact on your development database.
To run all backend tests, navigate to the /backend directory and run:
npm testThese tests verify that individual React components render and behave correctly in isolation. We use Vitest and React Testing Library for this.
To run all frontend unit tests, navigate to the /frontend directory and run:
npm testE2E tests simulate real user workflows in a browser from start to finish. This helps catch bugs in critical user journeys like logging in, creating a post, or navigating the application.
To open the Cypress Test Runner, navigate to the /frontend directory and run:
npm run cypress:open