A basic social media backend developed with Node.js and Express, providing essential functionalities for building a social media application. Key features include user authentication, subscriptions, and post and video interactions, all stored and managed in MongoDB. Additional functionalities include password hashing with bcrypt, file uploads with multer, mongoDB aggregation pipeline and pagination for video content using mongooseAggregatePaginate
.
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:
-
Set up the Frontend Application:
- Use a frontend framework like React or Vue to create the UI for your social media platform.
- Ensure you handle the user interface for registration, login, and profile management.
-
Make HTTP Requests:
- The frontend will interact with the backend by making HTTP requests to the endpoints exposed by this backend.
- Use libraries like
axios
orfetch
to send requests and handle responses.
-
Authentication:
- When the user logs in, the frontend will send a POST request to the
/users/login
endpoint with the email and password. - The backend will return a JWT token, which the frontend can store (typically in localStorage or a cookie).
- For all subsequent requests, the frontend should include this JWT token in the
Authorization
header to authenticate requests.
- When the user logs in, the frontend will send a POST request to the
-
Profile Management:
- The frontend can allow users to upload avatars and update their details by making PUT requests to endpoints like
/users/update-avatar
or/users/update-details
.
- The frontend can allow users to upload avatars and update their details by making PUT requests to endpoints like
-
Subscription and Watch History:
- Users can subscribe to channels and view their watch history by interacting with the appropriate endpoints, such as
/users/watch-history
or/users/:username/channel-profile
.
- Users can subscribe to channels and view their watch history by interacting with the appropriate endpoints, such as
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. |
- MongoDB - A NoSQL database for easy scalability: MongoDB Official
- JWT (JSON Web Tokens) - For secure authentication: JWT.io
- Cloudinary - Cloud storage service for media: Cloudinary
- Express.js - Web framework for building RESTful APIs: Express.js Documentation
- Bcrypt.js - Library for securely hashing passwords: Bcrypt.js Documentation
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.
-
Create a new branch for your changes:
git checkout -b new-feature
-
Make your changes and commit them:
git commit -m "Added new feature"
-
Push to your fork:
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.