-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.text
More file actions
448 lines (308 loc) · 8.86 KB
/
Copy pathnotes.text
File metadata and controls
448 lines (308 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
SMART FILE SHARING SYSTEM BACKEND NOTES
============================================================
1. FULL BACKEND GRAPH
============================================================
Frontend / Postman
|
| sends HTTP request
v
src/index.js
|
| loads .env and starts database connection
v
src/db/index2.js
|
| connects backend with MongoDB
v
src/app.js
|
| adds middleware and sends request to route files
v
Routes
|
| /api/register -> src/routes/user.routes.js
| /api/login -> src/routes/user.routes.js
| /api/profile -> src/routes/user.routes.js
| /api/auth/... -> src/routes/auth.routes.js
v
Controllers
|
| registerUser -> create account
| loginUser -> check password and create token
| getCurrentUser -> return logged-in user
v
Model
|
| src/modules/user.model.js
| defines user fields and password methods
v
MongoDB
|
| saves user data
v
Response
|
| ApiResponse sends success response
| ApiError is used when something goes wrong
v
Frontend / Postman receives JSON
============================================================
2. BACKEND EXPLAINED IN VERY EASY LANGUAGE
============================================================
Backend means the server side of your project.
The frontend or Postman sends a request like:
POST /api/register
The backend receives that request, checks the data, talks to the database,
and sends a response.
Example:
User enters username, email, password.
Backend checks if fields are empty.
Backend checks if email already exists.
Backend saves user in MongoDB.
Backend sends success response.
============================================================
3. FILE BY FILE EXPLANATION
============================================================
src/index.js
This is the starting file.
1. It loads .env values.
2. It imports connectDB.
3. It imports app.
4. It connects MongoDB.
5. It starts the server.
Easy meaning:
index.js turns on the backend.
src/app.js
This file creates the Express app.
1. express.json reads JSON from frontend.
2. cors allows frontend to call backend.
3. cookieParser reads cookies.
4. app.use("/api", userRoutes) connects user routes.
5. app.use("/api/auth", authRoutes) connects auth routes.
Easy meaning:
app.js decides which route file should handle each request.
src/db/index2.js
This file connects backend to MongoDB.
1. It uses mongoose.
2. It reads MONGO_URI from .env.
3. It tries to connect.
4. It prints success or error.
Easy meaning:
Without this file, your backend cannot save or read users from MongoDB.
src/routes/user.routes.js
This file defines user API URLs.
1. /register calls registerUser.
2. /login calls loginUser.
3. /profile calls getCurrentUser.
Easy meaning:
Routes are like road signs. They send requests to the correct controller.
src/controllers/user.controler.js
This file contains the real user logic.
registerUser:
1. Get data from req.body.
2. Check empty fields.
3. Check if user already exists.
4. Create user in MongoDB.
5. Password is hashed by user.model.js.
6. Send user data without password.
loginUser:
1. Get email and password.
2. Check empty fields.
3. Find user by email.
4. Compare password.
5. Create JWT token.
6. Send token to frontend.
getCurrentUser:
1. Sends current user data.
2. Later this should use auth middleware.
src/modules/user.model.js
This file defines how a user looks in MongoDB.
User fields:
1. username
2. email
3. password
4. fileUrl
5. fileId
6. files
It also hashes password before saving.
Easy meaning:
Model is the database structure.
src/utils/ApiResponse.js
This creates a common success response.
Example:
{
statusCode: 200,
message: "Login successful",
data: {...},
success: true
}
src/utils/ApiError.js
This creates a common error object.
Example:
throw new ApiError(400, "All fields are required")
src/utils/asyncHandler.js
This catches errors from async controllers.
Easy meaning:
It saves you from writing try/catch again and again.
src/middleware/multer.middleware.js
This handles file upload from frontend.
1. It stores uploaded file in public/temp.
2. It keeps original file name.
3. It exports upload middleware.
src/utils/cloudnary.js
This uploads files to Cloudinary.
1. Configure Cloudinary from .env.
2. Upload local file.
3. Return file URL and public id.
4. Delete local file if upload fails.
src/controllers/file.controller.js
This is draft file upload logic.
Future flow:
1. Get file from req.file.
2. Upload to Cloudinary.
3. Save URL in MongoDB.
4. Send share link.
src/routes/file.routes.js
This is draft file upload route.
It is commented because it still needs proper imports and auth middleware.
============================================================
4. REGISTER FLOW IN EASY LANGUAGE
============================================================
POST /api/register
1. Frontend sends username, email, password.
2. user.routes.js receives /register route.
3. It calls registerUser controller.
4. Controller checks empty fields.
5. Controller checks email in MongoDB.
6. Controller creates user.
7. user.model.js hashes password before saving.
8. MongoDB saves user.
9. Backend sends success response.
============================================================
5. LOGIN FLOW IN EASY LANGUAGE
============================================================
POST /api/login
1. Frontend sends email and password.
2. user.routes.js receives /login route.
3. It calls loginUser controller.
4. Controller finds user by email.
5. Controller compares password using bcrypt.
6. If password is correct, JWT token is created.
7. Backend sends token and user data.
============================================================
6. FILE UPLOAD FLOW IN EASY LANGUAGE
============================================================
Frontend / Postman uploads file
|
v
file.routes.js
|
v
multer.middleware.js
|
v
req.file
|
v
cloudnary.js uploads to Cloudinary
|
v
MongoDB saves URL
|
v
Backend sends response
Current status:
File upload flow is planned, but controller and route are not fully implemented yet.
============================================================
7. IMPLEMENTATION STEPS IN EASY LANGUAGE
============================================================
User register implementation:
// 1. Get data from frontend (req.body)
// 2. Validate fields
// - empty?
// - valid email?
// - password length?
// 3. Check if user already exists
// - email
// - username
// 4. Save password in model, model hashes it
// 5. Create user in database
// 6. Create clean response object
// 7. Remove password from response
// 8. Check if user created successfully
// 9. Send response
User login implementation:
// 1. Get email and password from frontend
// 2. Validate fields
// 3. Find user by email
// 4. Compare password
// 5. Create JWT token
// 6. Send response
File upload implementation:
// 1. Get file from frontend (req.file)
// 2. Validate file exists
// 3. Upload file to Cloudinary
// 4. Save file URL and public id in database
// 5. Generate shareable link
// 6. Send response
Database implementation:
// 1. Read MongoDB URL from .env
// 2. Call mongoose.connect
// 3. If connected, continue backend
// 4. If failed, show error
Route implementation:
// 1. Receive request URL
// 2. Match URL with route
// 3. Run middleware if needed
// 4. Run controller function
// 5. Controller sends response
============================================================
8. IMPORTANT SIMPLE DEFINITIONS
============================================================
req.body:
Data sent by frontend, like username, email, password.
req.file:
Uploaded file data added by multer.
Controller:
Function that contains main logic.
Route:
URL path that calls a controller.
Model:
Database structure.
Middleware:
Function that runs between request and controller.
JWT:
Login token used to prove user is logged in.
bcrypt:
Library used to hash and compare passwords.
Cloudinary:
Cloud storage for uploaded files.
MongoDB:
Database where user and file data is saved.
After deployment: after deployment i can do ??
Yes, after deployment your QR feature becomes a real-world feature. 🚀
AFTER MAKING THIS PROJECT I WILL WORK ON IT:
1. Deploy it
2. Learn Docker
3. Build one more backend project
4. Learn AWS basics (EC2 + S3)
5. Learn GitHub Actions CI/CD
6. Learn System Design basics
7. Learn Kubernetes
1. Push Backend to GitHub
2. Deploy Backend on Render
3. Test APIs on Render
4. Push Frontend to GitHub
5. Deploy Frontend on Vercel
6. Update API URL
7. Update CORS
8. Final Testing
Auth register/login
-> JWT middleware done
-> protected current-user route done
-> file model done
-> upload route
-> Cloudinary upload
-> save file record
-> share link
-> access shared file