RESTful backend using Node.js (Express), JWT authentication, bcrypt for credential hashing, Multer for streaming multipart uploads, Mongoose for ODM over MongoDB, S3-compatible media storage abstraction, and indexed cursor-based video pagination with optimized query performance.
Technology | Description |
---|---|
Node.js | JavaScript runtime used for server-side applications (Node.js Official Site). |
Express.js | A web framework for creating RESTful APIs (Express.js Documentation). |
MongoDB | NoSQL database for storing user data and other platform-related information (MongoDB Official). |
Mongoose | ODM for MongoDB, used to interact with the database easily (Mongoose Documentation). |
JWT | JSON Web Tokens for secure user authentication (JWT Documentation). |
Bcrypt.js | Library used to hash and verify passwords (Bcrypt.js Documentation). |
Multer | Middleware for handling multipart file uploads (such as images) (Multer Documentation). |
Cloudinary | Cloud storage service for storing profile images and cover photos (Cloudinary Official). |
Feature | Description |
---|---|
User Registration | Allows users to register using email/username and a secure password. |
User Authentication | Authenticates users using JWT tokens to allow secure access to resources. |
Password Management | Users can change their password securely using hashed password storage. |
Profile Management | Users can update their profile details, including avatar and cover photo. |
Subscription | Allows users to subscribe to channels and manage their subscriptions. |
Watch History | Tracks the videos a user has watched and stores them in their history. |
Clone the repository:
git clone https://github.com/your-username/social-media-backend.git
Navigate into the project directory:
cd social-media-backend
Install dependencies:
npm install
Create a .env
file for your environment variables:
MONGODB_URI=<Your MongoDB URI>
JWT_SECRET=<Your JWT Secret Key>
REFRESH_TOKEN_SECRET=<Your Refresh Token Secret>
CLOUDINARY_CLOUD_NAME=<Your Cloudinary Cloud Name>
CLOUDINARY_API_KEY=<Your Cloudinary API Key>
CLOUDINARY_API_SECRET=<Your Cloudinary API Secret>
Start the server:
npm start
To use this backend, connect it to a frontend framework (e.g., React, Vue, or Angular) to build a fully functional social media application. All endpoints are RESTful, allowing you to seamlessly integrate this backend with various frontend libraries. Below are a few steps to help you connect the backend with the frontend:
axios
or fetch
to send requests and handle responses./users/login
endpoint with the email and password.Authorization
header to authenticate requests./users/update-avatar
or /users/update-details
./users/watch-history
or /users/:username/channel-profile
.Method | Route | Description |
---|---|---|
POST | /users/register |
Registers a new user with details. |
POST | /users/login |
Logs in a user and returns a JWT token. |
POST | /users/logout |
Logs out the user by clearing the session. |
POST | /users/refresh-token |
Refreshes the access token using the refresh token. |
Method | Route | Description |
---|---|---|
GET | /users/me |
Retrieves the profile of the current user. |
PUT | /users/update-details |
Updates user details such as name and email. |
PUT | /users/change-password |
Allows the user to change their password. |
PUT | /users/update-avatar |
Uploads and updates the user’s avatar. |
PUT | /users/update-cover-image |
Uploads and updates the user’s cover image. |
Method | Route | Description |
---|---|---|
GET | /users/:username/channel-profile |
Fetches the user’s channel profile. |
GET | /users/watch-history |
Retrieves the user’s watch history. |
Error Code | Description |
---|---|
400 |
Bad Request - Invalid or missing data. |
401 |
Unauthorized - Invalid credentials or authentication. |
404 |
Not Found - Resource not found (e.g., user, video). |
500 |
Internal Server Error - Unexpected server error. |
Feel free to contribute to this project by creating issues, submitting pull requests, or suggesting improvements. If you would like to contribute, please:
Fork the repository and clone it to your local machine.
git checkout -b new-feature
git commit -m "Added new feature"
git push origin new-feature
5 . Open a pull request for review.
This project is licensed under the MIT License - see the LICENSE file for details.