Skip to content

coder-591/Backend-code-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Steps for user creation

1- get user details from frontend
2- validation - not empty
3- check if user is already exists: username, email
4- check for images,check for avatar
5- upload them cloudinary,avatar
6- create user object - create entry in db
7- remove password and refresh token field from response
8- check user creation
9- return res

Multer middleware or package

Multer looks at the incoming request.If the "Content-Type" is "multipart/form-data", it parses it.
For each file field, Multer:
Reads the binary data.
Stores the file (in memory or disk, depending on config).
Fills req.file (for single) or req.files (for multiple) with metadata like:
Multipart format:
This format contains binary file data + text fields together in chunks.

.env Errors

1- Using wrong .env variable names.
2- .env file not being loaded (forgot dotenv.config() in your server.js).
3- Putting credentials in process.env but not restarting the server.

TypeError: Cannot read properties of undefined

  • This error come from optional chaining or checking

How data flows from front-end to back-end

1.User Fills the Login Form

2. Browser Creates an HTTP Request
Request contains that what type of method you use e.g(get,post, etc.) ?, what type of host is ?,What type of content of the data and its length.Finally the data you fill in login form

1- POST /login HTTP/1.1
2- Host: localhost:5000
3- Content-Type: application/json
4- Content-Length: 42
5- { "email": "test@gmail.com", "password": "12345" }

3.Data Travels Over the Network

The browser sends this HTTP request over TCP/IP → goes through your OS → network stack → reaches your backend server (e.g. Express).

4.Server Receives Request

In Node.js (Express), the server sees:
1- req.method → POST
2- req.url → /login
3- req.headers → { "content-type": "application/json", ... }
4- req.body → not ready yet (still a raw stream!)

5.Express Middleware Parses Body

When you add in your code it parses or read the raw data and convert into json format or JS object and lastly it attach the converted data with req.body => it is by default "undefiend".

  • app.use(express.json());

Flow in One Line

  • Form → Browser builds HTTP request → Sends over network → Server receives raw stream → Middleware parses → Data attached to req.body.

About request and response objects

1- req and res are created by Node.js http server.
2- Express wraps & extends them with extra methods.
3- They are passed automatically into your route handler.
4- You don’t define them → they are provided by default per request.

About Aggregate pinelines

Here the you can learn about pinelines:

https://www.mongodb.com/docs/manual/core/aggregation-pipeline/#aggregation-pipeline

About

The code that I practice while learning backend

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published