0% found this document useful (0 votes)
2 views3 pages

Express Sir Notes

The document outlines the structure and components of a Node.js/Express application, detailing key files such as package.json, node_modules, App.js, .env, and others. It explains their purposes, including configuration management, data handling, and middleware functionality. Additionally, it covers security practices using JWT for authentication, bcrypt for password hashing, and Multer for file uploads.

Uploaded by

eng21cs0213
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Express Sir Notes

The document outlines the structure and components of a Node.js/Express application, detailing key files such as package.json, node_modules, App.js, .env, and others. It explains their purposes, including configuration management, data handling, and middleware functionality. Additionally, it covers security practices using JWT for authentication, bcrypt for password hashing, and Multer for file uploads.

Uploaded by

eng21cs0213
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

package.

json

• Acts as the main configuration file for the [Link] / Express project.
• Stores project metadata like name, version, and description.
• Lists dependencies and devDependencies required by the app.
• Defines scripts to run, build, or develop the application.

node_modules

• Contains all installed third-party packages used by the project.


• Automatically generated when npm install is executed.
• Used by [Link] to resolve require() or import statements.
• Should never be edited manually.
• Must be ignored in Git and regenerated when needed.

[Link] or [Link]

• Acts as the entry point of the Express application.


• Applies global middleware like JSON parsing and security.
• Registers route files with base paths.
• Loads environment variables and initializes configurations (like database
connections).
• Starts the HTTP server using [Link]() on a specified port.
• Handles app startup logic but does not contain route or business logic.

.env

• Stores environment-specific variables such as ports, database URLs, and secret


keys.
• Helps keep sensitive information (passwords, tokens, API keys) out of source code.
• Makes the application portable across environments (development, testing,
production).
• Values in this file are loaded into [Link] using libraries like dotenv.
• Should never be committed to Git and is always listed in .gitignore.

config

• Stores configuration logic.


• Manages database connections and environment settings.
• Reads values from .env files.
• Keeps configuration separate from business logic.

models

• Defines the structure of data stored in the database.


• Contains schemas, data types, and validation rules.
• Acts as a bridge between the app and the database.
• Ensures consistent and organized data handling.
• Used by controllers to perform database operations.

controllers
• Contains the business logic of the application.
• Receives requests from routes and prepares responses.
• Interacts with models to read or write data.
• Handles success and error cases in one place.
• Keeps routes clean and logic centralized.

routes

• Defines API endpoints and HTTP methods.


• Maps URLs to specific controller functions.
• Uses Express Router to organize endpoints.
• Can attach middleware to specific routes.
• Imported and registered inside [Link].

middleware

• Executes code between request and response.


• Used for authentication, validation, logging, and errors.
• Can modify request or response objects.
• Controls whether a request continues or stops.
• Reusable across multiple routes or globally.

JWT:

JWT is a token that is given to a user after they log in, so they don’t need to enter their
password again on every request.

It contains 3 parts — Header, Payload (user data), and Signature — combined into one string.

The server creates the token using [Link](), and the client sends it back on each request
(usually in headers or HTTP-only cookies).

The server checks the token using [Link]() to confirm that the user is real and the token
hasn’t been changed.

Tokens usually have an expiration time (e.g., 1 hour) to make them more secure.

bcrypt:

bcrypt is used to protect passwords before storing them in the database.

When a user signs up, bcrypt takes their real password and scrambles it into a long,
unreadable string called a hash.

The hashed password cannot be converted back to the original password — this keeps user
data safe.

bcrypt also adds random extra characters called “salt” to make the hashed password unique
and harder to crack.
When a user logs in, bcrypt takes the entered password, hashes it again, and checks if it
matches the stored hashed version.

This way, your database never stores real passwords, so even if hacked, the real passwords
stay safe.

Multer:

Multer is a tool that helps your server receive files from a user, like photos, videos, or
documents.

When someone uploads a file through a form, Express alone cannot read that file — Multer
makes it possible.

Multer takes the uploaded file and saves it somewhere — usually in a folder in your project
(like /uploads).

After saving the file, Multer gives you information about it (name, size, location) through
[Link](for single file) and [Link](for multiple files).

You can tell Multer to allow only certain types of files (like only images) and to limit file
sizes so users don’t upload huge files.

Multer is used mostly when building features like profile picture upload, product images, post
attachments, etc.

You might also like