Skip to content

Repository files navigation

ConnectHub Backend

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.

Features

  • 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

Setup

npm install
copy .env.example .env
npm start

Update .env with strong JWT secrets before running in production.

Development

npm run dev

Seed Data

Start MongoDB, then run:

npm run seed

Seeded users use password Password123!.

Docker

copy .env.example .env
docker compose up --build

For Docker, set MONGODB_URI=mongodb://mongo:27017/connecthub in .env.

API Prefix

All routes are mounted under:

/api/v1

Health check:

GET /health

Main Endpoints

Auth:

  • POST /api/v1/auth/register
  • POST /api/v1/auth/login
  • POST /api/v1/auth/refresh
  • POST /api/v1/auth/change-password

Users:

  • GET /api/v1/users/me
  • GET /api/v1/users/:id
  • PUT /api/v1/users/update

Posts:

  • POST /api/v1/posts
  • GET /api/v1/posts/:id
  • DELETE /api/v1/posts/:id
  • GET /api/v1/posts/user/:userId

Interactions:

  • POST /api/v1/posts/:id/like
  • POST /api/v1/posts/:id/unlike
  • POST /api/v1/posts/:id/comment
  • GET /api/v1/posts/:id/comments
  • POST /api/v1/posts/:id/bookmark
  • DELETE /api/v1/posts/:id/bookmark
  • GET /api/v1/posts/bookmarks/me

Follow:

  • POST /api/v1/follow/request
  • POST /api/v1/follow/accept
  • POST /api/v1/follow/reject
  • POST /api/v1/follow/unfollow

Notifications:

  • GET /api/v1/notifications
  • PATCH /api/v1/notifications/:id/read
  • PATCH /api/v1/notifications/read-all

Feed:

  • GET /api/v1/feed/personalized
  • GET /api/v1/feed/global
  • GET /api/v1/feed/explore

Error Format

{
  "error": {
    "code": "STRING_CODE",
    "message": "Human readable message"
  }
}

Real-Time Notifications

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);
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages