A robust social media backend API built with Node.js, Express.js, and MongoDB. This project combines features from both YouTube and Twitter, allowing users to create content, subscribe to channels, and interact with posts.
- User Registration & Authentication with JWT tokens
- Profile Management with avatar and cover image upload
- Secure Password Handling with bcrypt encryption
- Access & Refresh Token implementation for enhanced security
- Tweet Creation with text content
- Media Upload integration with Cloudinary
- User Profiles with follower/following statistics
- Channel Subscriptions system
- MongoDB Aggregation Pipelines for complex data queries
- Subscription Analytics using advanced pipeline operations
- File Upload Handling with Multer middleware
- Error Handling with custom error classes
- Input Validation and sanitization
- Node.js - Runtime environment
- Express.js - Web application framework
- MongoDB - NoSQL database
- Mongoose - MongoDB object modeling
- JWT (JSON Web Tokens) - Authentication tokens
- bcrypt - Password hashing
- Cookie Parser - Cookie handling middleware
- Multer - File upload middleware
- Cloudinary - Cloud-based image/video management
- Nodemon - Development server auto-restart
- dotenv - Environment variable management
- CORS - Cross-origin resource sharing
src/
βββ controllers/ # Route handlers
β βββ user.controller.js # User-related operations
βββ middlewares/ # Custom middleware
β βββ auth.middleware.js # JWT authentication
β βββ multer.middleware.js # File upload handling
βββ models/ # Database schemas
β βββ user.model.js # User schema
β βββ tweet.model.js # Tweet schema
β βββ subscription.model.js # Subscription schema
β βββ like.model.js # Like schema
β βββ comment.model.js # Comment schema
β βββ playlist.model.js # Playlist schema
β βββ video.model.js # Video schema
βββ routes/ # API routes
β βββ user.routes.js # User-related routes
βββ utils/ # Utility functions
β βββ ApiError.js # Custom error handling
β βββ ApiResponse.js # Standardized API responses
β βββ asyncHandler.js # Async error wrapper
β βββ cloudinary.js # Cloudinary configuration
βββ db/ # Database configuration
β βββ index.js # MongoDB connection
βββ app.js # Express app configuration
POST /api/v1/users/register # User registration
POST /api/v1/users/login # User login
POST /api/v1/users/logout # User logout
POST /api/v1/users/refresh-token # Refresh access token
GET /api/v1/users/current-user # Get current user details
POST /api/v1/users/change-password # Change user password
PATCH /api/v1/users/update-account # Update account details
PATCH /api/v1/users/avatar # Update user avatar
PATCH /api/v1/users/cover-image # Update cover image
GET /api/v1/users/c/:username # Get user channel profile
GET /api/v1/users/history # Get watch history
- Node.js (v14 or higher)
- MongoDB (local or Atlas)
- Cloudinary account for media storage
-
Clone the repository
git clone https://github.com/somyaknotfound/youtube-twitter.git cd youtube-twitter -
Install dependencies
npm install
-
Environment Configuration Create a
.envfile in the root directory:PORT=8000 MONGODB_URI=mongodb://localhost:27017/youtube-twitter CORS_ORIGIN=* ACCESS_TOKEN_SECRET=your-access-token-secret ACCESS_TOKEN_EXPIRY=1d REFRESH_TOKEN_SECRET=your-refresh-token-secret REFRESH_TOKEN_EXPIRY=10d CLOUDINARY_CLOUD_NAME=your-cloudinary-name CLOUDINARY_API_KEY=your-cloudinary-api-key CLOUDINARY_API_SECRET=your-cloudinary-api-secret
-
Start the development server
npm run dev
-
Access the API
Server runs on: http://localhost:8000 Test route: http://localhost:8000/api/v1/users/test
{
username: String (unique, lowercase)
email: String (unique, lowercase)
fullName: String
avatar: String (Cloudinary URL)
coverImage: String (Cloudinary URL)
watchHistory: [ObjectId] (ref: Video)
password: String (hashed)
refreshToken: String
}{
content: String
owner: ObjectId (ref: User)
createdAt: Date
updatedAt: Date
}{
subscriber: ObjectId (ref: User)
channel: ObjectId (ref: User)
createdAt: Date
}The project extensively uses MongoDB aggregation pipelines for:
- User Channel Analytics: Complex queries to fetch subscriber counts, subscription status
- Content Statistics: Advanced data aggregation for user profiles
- Relationship Mapping: Efficient subscriber-channel relationship queries
const channel = await User.aggregate([
{ $match: { username: username.toLowerCase() } },
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "channel",
as: "subscribers"
}
},
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "subscriber",
as: "subscribedTo"
}
},
{
$addFields: {
subscribersCount: { $size: "$subscribers" },
channelSubscribedToCount: { $size: "$subscribedTo" },
isSubscribed: {
$cond: {
if: { $in: [req.user?._id, "$subscribers.subscriber"] },
then: true,
else: false
}
}
}
}
]);- JWT Authentication with access and refresh tokens
- Password Encryption using bcrypt
- File Upload Security with Multer configurations
- Input Validation and sanitization
- CORS Configuration for cross-origin requests
- Environment Variables for sensitive data
- Verifies JWT tokens from cookies or headers
- Adds user information to request object
- Handles token expiration and refresh
- Handles multipart/form-data requests
- Integrates with Cloudinary for cloud storage
- Supports both avatar and cover image uploads
- MongoDB Aggregation Pipelines for complex data operations
- JWT Authentication implementation with refresh tokens
- File Upload handling with cloud storage integration
- RESTful API design and development
- Error Handling and middleware pattern implementation
- Database Modeling for social media applications
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is based on the tutorial series from:
Chai aur Code - JavaScript Backend Development
- Instructor: Hitesh Choudhary
- Channel: https://www.youtube.com/@chaiaurcode
Somyak - GitHub Profile
If you have any questions or need help with setup, please open an issue in the GitHub repository.
β Star this repository if you found it helpful!