ConnectHub is a production-ready REST API backend for a social media platform built with Node.js, Express, MongoDB, Mongoose, JWT authentication, and a clean MVC plus service layer architecture.
- JWT access and refresh token authentication
- Register, login, refresh token, and change password
- User profiles, profile updates, and public user lookup
- Posts with media URLs, counters, and indexed author queries
- Likes, comments, and bookmarks with duplicate prevention
- Follow requests, accept/reject flow, and unfollow
- Event-driven notifications for likes, comments, follow requests, and accepted follows
- Optional Socket.IO real-time notification delivery
- Personalized, global trending, and explore feeds
- Cursor-based pagination
- Joi request validation
- Centralized JSON logging
- Security middleware, rate limiting, NoSQL injection sanitization, and global error handling
- Docker and seed script included
npm install
copy .env.example .env
npm startUpdate .env with strong JWT secrets before running in production.
npm run devStart MongoDB, then run:
npm run seedSeeded users use password Password123!.
copy .env.example .env
docker compose up --buildFor Docker, set MONGODB_URI=mongodb://mongo:27017/connecthub in .env.
All routes are mounted under:
/api/v1
Health check:
GET /health
Auth:
POST /api/v1/auth/registerPOST /api/v1/auth/loginPOST /api/v1/auth/refreshPOST /api/v1/auth/change-password
Users:
GET /api/v1/users/meGET /api/v1/users/:idPUT /api/v1/users/update
Posts:
POST /api/v1/postsGET /api/v1/posts/:idDELETE /api/v1/posts/:idGET /api/v1/posts/user/:userId
Interactions:
POST /api/v1/posts/:id/likePOST /api/v1/posts/:id/unlikePOST /api/v1/posts/:id/commentGET /api/v1/posts/:id/commentsPOST /api/v1/posts/:id/bookmarkDELETE /api/v1/posts/:id/bookmarkGET /api/v1/posts/bookmarks/me
Follow:
POST /api/v1/follow/requestPOST /api/v1/follow/acceptPOST /api/v1/follow/rejectPOST /api/v1/follow/unfollow
Notifications:
GET /api/v1/notificationsPATCH /api/v1/notifications/:id/readPATCH /api/v1/notifications/read-all
Feed:
GET /api/v1/feed/personalizedGET /api/v1/feed/globalGET /api/v1/feed/explore
{
"error": {
"code": "STRING_CODE",
"message": "Human readable message"
}
}Socket.IO is attached to the HTTP server. Clients can join their user room with:
const socket = io('http://localhost:5000', {
query: { userId: '<authenticated-user-id>' }
});
socket.on('notification', (notification) => {
console.log(notification);
});