A scalable social media backend service built with Express.js, Redis, RabbitMQ, and modern tools, featuring rate limiting and a microservice architecture with asynchronous messaging.
- 🔐 Secure Authentication – JWT-based authentication with access and refresh tokens
- 🧾 User Registration & Login – User signup, login, logout, and token refresh functionality
- 📝 Post Management System – Create, retrieve (single & paginated), and delete posts
- 🗂️ Media Handling – Upload media files and fetch uploaded media
- 🔍 Search & Indexing – Full-text search for posts with asynchronous indexing via RabbitMQ
- ⚡ Performance Optimized – Redis caching for tokens, posts, and frequently accessed data
- 🛡️ API Security – Rate limiting, Helmet, and CORS protection
- 🔄 Event-Driven Microservices – Asynchronous communication using RabbitMQ events (post.created, media.upload)
- 🧩 API Gateway Pattern – Centralized request routing to independently deployable services
- 📊 Logging & Monitoring – Centralized logging with Winston for better observability
+--------+ +-------------+ +-------------+ +--------------+ +--------------+ +-----------+
| Client | | API Gateway | | Post Service| |Search Service| | Media Service| | RabbitMQ |
+--------+ +-------------+ +-------------+ +--------------+ +--------------+ +-----------+
| | | | | |
| Create Post Requsest | | | | |
|--------------------->| | | | |
| | Forward Request | | | |
| |--------------------->| | | |
| | | | Publish post.created.event |
| | |------------------------------------------------------------------------>|
| | | | | |
| | Post Created Response| | | |
| |<---------------------| | | |
| | | | | |
| Post Created Response| | | | |
|<---------------------| | | | |
| | | | Consume post.created.event |
| | | |<-------------------------------------------------|
| | | | | |
| | | |---- | |
| | | | | Index new Post | |
| | | |<--- | |
| | | | | |
| | | | |Consume post.created.event|
| | | | |<-------------------------|
| | | | | |
| | | | |---- |
| | | | | | Process media |
| | | | |<--- |
| Search Requsest | | | | |
|--------------------->| | | | |
| | Forward Request | | |
| |-------------------------------------------->| | |
| | | | | |
| | Search Result | | |
| |<--------------------------------------------| | |
| | | | | |
| Search Requsest | | | | |
|<---------------------| | | | |
| Upload Media Requsest| | | | |
|--------------------->| | | | |
| | | | | |
| | | Forward Request | | |
| |-------------------------------------------------------------------->| |
| | | | | |
| | | | |Publish media.upload event|
| | | | |------------------------->|
| | | Upload Success respons | |
| |<--------------------------------------------------------------------| |
| | | | | |
| | | | | |
| Upload Success Res | | | | |
|<---------------------| | | | |
| | | | | |
| | | | Consume media.upload event |
| | |<------------------------------------------------------------------------|
| | | | | |
| | | | | |
| | |---- upload post | | |
| | | | with media | | |
| | |<--- | | |
| | | | | |
+--------+ +-------------+ +-------------+ +--------------+ +--------------+ +-----------+
| Client | | API Gateway | | Post Service| |Search Service| | Media Service| | RabbitMQ |
+--------+ +-------------+ +-------------+ +--------------+ +--------------+ +-----------+
- 🧠 Framework: Express.js
- 🗄️ Primary Database: MongoDB with Mongoose ORM
- ⚡ Caching: Redis
- 📊 Monitoring: Winston for logging
- 🔒 Security: Helmet, CORS, rate limiting
- 🪪 Validation: Joi
- 🐇 Message Broker: RabbitMQ for asynchronous communication
- 🐳 Containerization: Docker & Docker Compose
- Node.js (v18 or higher)
- Redis (v7+)
- MongoDB (v6+)
- Erlang/OTP (v28.3)
- RabbitMQ (v3.12+)
- Docker & Docker Compose (optional)
git clone https://github.com/soumadip-dev/SocialMedia-API.git
cd SocialMedia-APICreate a .env file in the api-gateway folder:
PORT=8080
NODE_ENV=development
IDENTITY_SERVICE_URL=http://localhost:8081
POST_SERVICE_URL=http://localhost:8083
MEDIA_SERVICE_URL=http://localhost:8082
SEARCH_SERVICE_URL=http://localhost:8084
REDIS_URL=redis://localhost:6379
FRONTEND_URL=http://localhost:3000
JWT_SECRET=super_strong_jwt_secret_key
Create a .env file in the identity-service folder:
NODE_ENV=development
PORT=8081
MONGO_URI=mongodb://localhost:27017/socialmedia_identity
JWT_SECRET=super_strong_jwt_secret_key
FRONTEND_URL=http://localhost:3000
REDIS_URL=redis://localhost:6379Create a .env file in the media-service folder:
NODE_ENV=development
PORT=8082
MONGO_URI=mongodb://localhost:27017/socialmedia_media
FRONTEND_URL=http://localhost:3000
REDIS_URL=redis://localhost:6379
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
RABBITMQ_URL=amqp://localhost:5672Create a .env file in the post-service folder:
NODE_ENV=development
PORT=8083
MONGO_URI=mongodb://localhost:27017/socialmedia_posts
FRONTEND_URL=http://localhost:3000
REDIS_URL=redis://localhost:6379
RABBITMQ_URL=amqp://localhost:5672Create a .env file in the search-service folder:
NODE_ENV=development
PORT=8084
MONGO_URI=mongodb://localhost:27017/socialmedia_search
FRONTEND_URL=http://localhost:3000
REDIS_URL=redis://localhost:6379
RABBITMQ_URL=amqp://localhost:5672Install dependencies for each service separately.
# API Gateway
cd api-gateway
npm install
# Identity Service
cd ../identity-service
npm install
# Media Service
cd ../media-service
npm install
# Post Service
cd ../post-service
npm install
# Search Service
cd ../search-service
npm installMake sure the following services are running locally before starting the application:
- MongoDB
- Redis
- RabbitMQ
Verify they are accessible on their default ports:
- MongoDB →
27017 - Redis →
6379 - RabbitMQ →
5672
Each microservice must be started in a different terminal window.
cd api-gateway
npm run devcd identity-service
npm run devcd media-service
npm run devcd post-service
npm run devcd search-service
npm run devAfter successful startup, the services will be available at:
- API Gateway:
http://localhost:8080 - Identity Service:
http://localhost:8081 - Media Service:
http://localhost:8082 - Post Service:
http://localhost:8083 - Search Service:
http://localhost:8084
The system is now ready to accept requests through the API Gateway.
POST /api/v1/auth/register– Register a new userPOST /api/v1/auth/login– Log in a userPOST /api/v1/auth/refresh-token– Refresh access tokenPOST /api/v1/auth/logout– Log out a user
POST /api/v1/post/create-post– Create a new postGET /api/v1/post/posts?page=&limit=– Get all posts (paginated)GET /api/v1/post/:id– Get a post by IDDELETE /api/v1/post/:id– Delete a post by ID
GET /api/v1/media– Get all media filesPOST /api/v1/media/upload– Upload media
GET /api/v1/search/posts?query=– Search posts
