Skip to content

Commit 6d67536

Browse files
authored
Create README.md
1 parent 2d1e295 commit 6d67536

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# js\_backend: YouTube-Style Backend API Clone
2+
3+
A **YouTube-style video hosting backend** built with Node.js, Express, and MongoDB. This RESTful API implements common features of a video platform (user accounts, video upload, likes, comments, playlists, etc.) as a learning project from the Chai aur Code course. Although there’s no frontend yet, the backend can be run locally and tested with tools like Postman. It includes controllers for users, videos, playlists, comments, likes, tweets (short posts), subscriptions, and a dashboard for stats. The code is organized with clear routes and models, and uses JWT for authentication, Multer for file uploads, and Cloudinary for media storage.
4+
5+
## Features
6+
7+
* **User**: User registration, login, profile updates and management. Passwords are hashed with `bcrypt`, and authentication is handled with JSON Web Tokens (JWT).
8+
* **Video**: Upload, fetch, update, and delete videos. Videos (and thumbnails) are uploaded via `Multer` and stored on Cloudinary. Public endpoints allow fetching video lists or details.
9+
* **Likes/Dislikes**: Users can like or unlike videos. The backend tracks likes for each video.
10+
* **Comments**: Users can add comments to videos. Each comment is linked to a video and author.
11+
* **Playlist**: Create and manage video playlists. Users can add or remove videos in their personal playlists.
12+
* **Tweets**: (Bonus social feature) Post short “tweets” or micro-posts, and fetch them. This adds a simple social feed on top of video content.
13+
* **Subscriptions**: Users can subscribe to other users (channels) and unsubscribe. Fetch a list of subscriptions or subscribers.
14+
* **Dashboard**: An endpoint for aggregated stats (e.g., total videos, total users, likes, etc.) or user-specific analytics. Useful for admin or user dashboards.
15+
16+
## Tech Stack
17+
18+
* **Node.js & Express**: Backend runtime and web framework.
19+
* **MongoDB (Mongoose)**: NoSQL database for storing users, videos, comments, etc.
20+
* **REST API Architecture**: HTTP endpoints returning JSON.
21+
* **Authentication**: JSON Web Tokens (JWT) for securing routes (login, protected actions).
22+
* **File Uploads**: `Multer` handles multipart form-data for uploading videos/images.
23+
* **Cloudinary**: Cloud service for storing and serving uploaded videos and images.
24+
* **Other Libraries**:
25+
26+
* `bcrypt` for hashing passwords.
27+
* `cors` and `cookie-parser` for handling cross-origin requests and cookies.
28+
* `dotenv` for environment variables.
29+
* `mongoose-aggregate-paginate-v2` for easy pagination of database queries.
30+
* `body-parser` (though Express has built-in parsing).
31+
* **Dev Tools**:
32+
33+
* `nodemon` for live-reloading during development.
34+
* `Prettier` for consistent code formatting.
35+
36+
## Installation & Setup
37+
38+
1. **Clone the repository** to your local machine:
39+
40+
```bash
41+
git clone https://github.com/your-username/js_backend.git
42+
cd js_backend
43+
```
44+
2. **Install dependencies**:
45+
46+
```bash
47+
npm install
48+
```
49+
3. **Configure environment variables**: Create a `.env` file in the project root (see below for required keys).
50+
4. **Start the development server**:
51+
52+
```bash
53+
npm run dev
54+
```
55+
56+
The server will start (e.g., on `http://localhost:5000/`) and watch for file changes. You can now send requests to the API.
57+
58+
## Environment Variables
59+
60+
The project uses a `.env` file to store sensitive information. Create a `.env` file with at least the following variables (you can adjust names to match your code):
61+
62+
* `PORT` – (Optional) Port number for the server (e.g., `5000`).
63+
* `MONGODB_URI` – MongoDB connection string (e.g., from MongoDB Atlas or local MongoDB).
64+
* `JWT_SECRET` – Secret key for signing JWT access tokens.
65+
* `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, `CLOUDINARY_API_SECRET` – Your Cloudinary credentials for uploading media.
66+
67+
*For example, your `.env` might look like:*
68+
69+
```env
70+
PORT=5000
71+
MONGODB_URI=mongodb+srv://username:[email protected]/mydatabase
72+
JWT_SECRET=your_jwt_secret_key
73+
CLOUDINARY_CLOUD_NAME=your_cloud_name
74+
CLOUDINARY_API_KEY=your_api_key
75+
CLOUDINARY_API_SECRET=your_api_secret
76+
```
77+
78+
## API Folder/Controller Structure
79+
80+
The project is organized with clear folders for each part of the API:
81+
82+
```
83+
js_backend/
84+
└── src/
85+
├── controllers/ # Logic for handling requests (e.g., userController.js, videoController.js, etc.)
86+
├── models/ # Mongoose schemas (e.g., User.js, Video.js, Comment.js, etc.)
87+
├── routes/ # Express route handlers mapping URLs to controller functions
88+
├── middleware/ # Custom middleware (e.g., auth.js for JWT verification)
89+
├── config/ # Configuration files (e.g., database connection, Cloudinary setup)
90+
└── server.js # Entry point: initializes Express app, connects to DB, and starts server
91+
```
92+
93+
* **controllers/**: Each file (like `videoController.js`) exports functions that implement the API operations for that feature.
94+
* **models/**: Define the data schemas with Mongoose (e.g., user schema, video schema).
95+
* **routes/**: Set up endpoints (e.g., `POST /api/videos` or `GET /api/users`) and connect them to the controller logic.
96+
* **middleware/**: Functions like `authMiddleware` check JWT tokens and protect certain routes.
97+
* **config/**: Handles external setup like connecting to MongoDB (`db.js`) and configuring Cloudinary credentials.
98+
* **server.js**: Loads environment variables, connects to the database, applies middleware (CORS, JSON parsing, cookie parsing), and starts listening on the specified port.
99+
100+
## How to Use the API
101+
102+
You can test the API using Postman or any HTTP client. Below are basic usage examples:
103+
104+
* **Register a new user**:
105+
`POST /api/users/register` with JSON body `{ "name": "Alice", "email": "[email protected]", "password": "secure123" }`.
106+
* **Login** (get JWT token):
107+
`POST /api/users/login` with `{ "email": "...", "password": "..." }`. The response contains a JWT.
108+
* **Authenticate requests**: Include the JWT in the `Authorization` header as `Bearer <token>` for protected routes (like uploading a video).
109+
* **Upload a video**:
110+
`POST /api/videos` – Use multipart/form-data with a `file` field for the video. (Requires auth.) You may also include fields like `title` and `description`.
111+
* **List videos**:
112+
`GET /api/videos` – Retrieves a list of all videos (public). Optional query params can be used for pagination or searching.
113+
* **Get video details**:
114+
`GET /api/videos/:id` – Fetch a single video's data by its ID.
115+
* **Like a video**:
116+
`POST /api/videos/:id/like` – Like or unlike the specified video (toggle action). (Requires auth.)
117+
* **Comment on a video**:
118+
`POST /api/videos/:id/comments` – Add a comment. Body example: `{ "text": "Nice video!" }`. (Requires auth.)
119+
* **Create a playlist**:
120+
`POST /api/playlists` – Create a new playlist. Body example: `{ "name": "My Favorites", "videoIds": ["id1", "id2"] }`. (Requires auth.)
121+
* **Subscribe to a user**:
122+
`POST /api/subscriptions` – Subscribe by providing a channel/user ID. (Requires auth.)
123+
* **Post a tweet**:
124+
`POST /api/tweets` – Create a short text post. Body example: `{ "text": "Hello world!" }`. (Requires auth.)
125+
* **View dashboard stats**:
126+
`GET /api/dashboard` – Retrieve overall statistics (for admin or own profile).
127+
128+
Each endpoint will return JSON responses. Be sure to set the `Content-Type` header to `application/json` for JSON requests, and use `multipart/form-data` for file uploads. Consult the controller and route definitions in the code for more details on request formats and response structures.
129+
130+
## Contributing
131+
132+
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. To contribute code:
133+
134+
1. Fork the repository.
135+
2. Create a new branch for your feature or bugfix.
136+
3. Make your changes, following the existing code style (Prettier is set up for formatting).
137+
4. Commit and push your branch.
138+
5. Open a pull request with a description of your changes.
139+
140+
We appreciate any feedback or improvements. This project is meant for learning and growth, so feel free to experiment and refine the code. 🙂
141+
142+
## License
143+
144+
This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)