A lightweight Q&A backend platform inspired by Stack Overflow. Users can ask questions, provide answers, vote on content, and track platform metrics. The application features real-time updates and caching mechanisms.
- Question and answer functionality
- Voting system
- User authentication via Auth0 OpenID Connect
- Real-time updates using WebSocket
- Redis caching for fetching popular questions page
- Usage metrics
- Complete Swagger documentation
- Backend: Node.js with Express
- Development: TypeScript
- Database: PostgreSQL with TypeORM
- Caching: Redis
- Authentication: Auth0 OpenID Connect
- Real-time updates: WebSocket
- API Documentation: Swagger
- Containerization: Docker
The application follows a layered architecture pattern, specifically implementing the Repository Pattern with Service Layer for a better separation of concerns and maintainable code structure.
- Routes Layer - HTTP request handling and routing
- Controllers Layer - Request/response handling and data validation
- Service Layer - Business logic implementation
- Repository Layer - Data access layer using TypeORM
Request -> Routes -> Controllers -> Services -> Repositories -> Database
All API endpoints are versioned under /v1
(e.g., /api/v1/questions
).
src/
|── config/
|── controllers/
|── middlewares/
|── models/
|── routes/
|── services/
|── types/
|── utils/
|── app.ts
- Config: Contains all configuration setup (database, authentication and Redis)
- Controllers: Handle HTTP requests and return responses
- Middlewares: Implement middlewares for authentication and caching
- Models: Define database entities using TypeORM decorators
- Routes: Define API endpoints and their corresponding controllers
- Services: Implement business logic and make data operations
- Types: Contains TypeScript interfaces and types
- Utils: Helper functions and utilities
The application implements real-time updates using WebSocket for events related to questions and answers. This ensures the client receives immediate updates without polling the server.
questionCreated
- When a new question is postedquestionUpdated
- When a question is editedquestionDeleted
- When a question is removedquestionVoted
- When a question receives a votequestionAnswered
- When an answer is added to a question
answerCreated
- When a new answer is postedanswerUpdated
- When an answer is editedanswerDeleted
- When an answer is removedanswerVoted
- When an answer receives a vote
Each event carries relevant data like ID, content, author information, and vote counts.
The application implements Redis caching to optimize performance for frequently accessed data.
- The first page of popular questions is cached using Redis
- Cache duration: 1 hour (3600 seconds)
- Cache key depends on the number of entries per page
- Cache is automatically invalidated when:
- New questions are created
- Questions are updated or deleted
- Voting occurs
- Checks if requested data exists in cache before querying database
- Caches database results for subsequent requests
- Includes cache invalidation mechanisms to ensure data consistency
- Docker and Docker Compose
- Node.js 18
- PostgreSQL 14
- Redis
-
Clone the repository
-
Copy .env.example to .env and configure environment variables
-
Start the Application (make sure Docker Desktop is running)
docker-compose up --build
This will start:
- Main application on port 3000
- PostgreSQL database on port 5432
- Redis on port 6379
- Access the Application
- API Documentation:
http://localhost:3000/api-docs
- Main API:
http://localhost:3000/
- Login:
http://localhost:3000/login
(redirects to Auth0 authentication form) - Logout:
http://localhost:3000/logout
The API is documented using Swagger. Once the application is running, visit /api-docs
for interactive API documentation.
If you're running locally:
npm run test
If you're running in Docker:
docker-compose exec stackoverflowlight npm run test
The project includes three types of tests:
- Unit tests: Testing individual components and functions
- Integration tests: Testing database operations and repositories
- E2E tests: Testing API endpoints and request flows
Note: Tests use a separate test database (running on port 5433) to avoid interfering with the main application database.
This application can be deployed to AWS using Fargate - a serverless container service that eliminates the need to manage servers.
- Container Registry: AWS ECR to store Docker images
- Database: PostgreSQL on Amazon RDS
- Cache: Redis on ElastiCache
- Application Hosting: AWS Fargate
- Initial Setup
- Set up AWS infrastructure
- Create database and Redis instances
- Configure container registry
- Application Deployment
- Build and push Docker image
- Configure environment variables
- Deploy as Fargate service
- Set up load balancer for traffic distribution
The application is designed to scale automatically based on load. Fargate handles container scaling while RDS and ElastiCache provide database and cache scaling respectively.