Node.js + Express.js Cheat Sheet
PART 1 — Node.js Fundamentals
1. What Is Node.js?
Node.js lets you run JavaScript on the server instead of the browser.
It’s built for fast, scalable backend applications using non-blocking I/O.
2. Why Node.js?
Node.js is popular because:
-
Uses JavaScript everywhere
-
Handles many users efficiently
-
Huge npm ecosystem
-
Perfect for APIs and real-time apps
3. Installing Node
After installing Node:
node -v npm -v
These commands verify Node and npm.
4. Running a File
node app.js
Executes your JavaScript file on the server.
5. Basic Node Program
console.log("Hello Node");
Node runs JavaScript line by line.
6. Modules
Node uses modules to organize code.
CommonJS Import
const fs = require("fs");
Export
module.exports = myFunc;
7. ES Modules
Modern syntax:
import fs from "fs";
Requires "type":"module" in package.json.
8. Built-in Modules
Common ones:
fs → files
path → paths
http → server
os → system info
Used for core backend work.
9. File System (fs)
Read file:
fs.readFileSync("a.txt")
Write file:
fs.writeFileSync("a.txt","hi")
Used for file operations.
10. Path Module
path.join(__dirname,"file.txt")
Builds safe file paths.
11. Process Object
process.env.PORT
Access environment variables.
12. package.json
Project configuration file.
Contains:
-
dependencies
-
scripts
-
project metadata
13. npm
Node package manager.
npm install express
Downloads libraries.
14. nodemon
Auto-restarts server on code change.
npm i nodemon
Used during development.
PART 2 — Express.js Basics
15. What Is Express?
Express is a lightweight Node framework for building REST APIs and web servers.
It simplifies routing, middleware, and requests.
16. Installing Express
npm install express
Adds Express to project.
17. Basic Server
const express = require("express");
const app = express();
app.listen(3000);
Starts HTTP server.
18. Simple Route
app.get("/",(req,res)=>{
res.send("Hello");
});
Responds to browser requests.
19. Request Object (req)
Contains client data:
-
params
-
query
-
body
-
headers
20. Response Object (res)
Used to send data back.
res.json()
res.status()
res.send()
21. HTTP Methods
GET → read
POST → create
PUT → update
DELETE → remove
Core REST operations.
22. JSON Middleware
app.use(express.json());
Allows reading JSON body.
Required for APIs.
PART 3 — Routing
23. Route Parameters
/users/:id
Access via:
req.params.id
Used for dynamic routes.
24. Query Parameters
/users?page=1
Access:
req.query.page
Used for filters.
25. Router
const router = express.Router();
Splits routes into files.
Improves structure.
26. Route Files
routes/ controllers/
Professional project separation.
PART 4 — Middleware
27. What Is Middleware?
Middleware runs between request and response.
Used for:
-
auth
-
logging
-
validation
28. Custom Middleware
app.use((req,res,next)=>{
next();
});
next() passes control.
29. Third-Party Middleware
Examples:
cors
morgan
helmet
Add security and logging.
30. Error Middleware
app.use((err,req,res,next)=>{})
Central error handling.
PART 5 — MVC Architecture
31. Model
Database logic.
32. Controller
Business logic.
33. Routes
URL definitions.
This separation keeps backend clean and scalable.
PART 6 — Database Integration
34. MongoDB + Mongoose
Popular NoSQL combo.
mongoose.connect()
35. Schema
Defines data structure.
new Schema({})
36. Model
Represents collection.
mongoose.model()
37. CRUD Operations
create()
find()
findById()
updateOne()
deleteOne()
Core database actions.
38. SQL Databases
Use:
PostgreSQL
MySQL
Via libraries like pg or sequelize.
PART 7 — Authentication
39. JWT
JSON Web Token for login sessions.
40. Login Flow
User login
Generate token
Send token
Verify token
Standard API auth.
41. Password Hashing
bcrypt.hash()
Never store plain passwords.
42. Auth Middleware
Protect routes.
verifyToken()
PART 8 — Validation
43. express-validator
Validates request input.
Prevents bad data.
PART 9 — File Upload
44. Multer
Handles file uploads.
multer()
Used for images/docs.
PART 10 — Environment Variables
45. dotenv
require("dotenv").config();
Stores secrets safely.
PART 11 — Async Programming
46. async / await
await dbCall();
Simplifies promises.
47. try / catch
Handles async errors.
PART 12 — Logging
48. Console Logs
Basic debugging.
49. Winston
Professional logging.
Used in production.
PART 13 — Testing
50. Jest
Unit testing.
51. Supertest
API testing.
PART 14 — Security
52. Helmet
Sets HTTP headers.
53. CORS
Controls cross-origin requests.
54. Rate Limiting
Prevents abuse.
PART 15 — Deployment
55. Build Server
node index.js
56. Production Tools
PM2
Docker
Nginx
Cloud
57. Reverse Proxy
Nginx routes traffic.
PART 16 — Advanced Topics
58. WebSockets
Real-time communication.
59. Redis
Caching layer.
60. Kafka
Message queues.
61. Microservices
Split backend into services.