This cheat sheet is designed to help you learn Node.js in 30 days. It covers the basics, intermediate, and advanced topics, along with code snippets and key concepts.
Day 1-5: Node.js Basics
-
What is Node.js?
- JavaScript runtime built on Chrome's V8 engine.
- Event-driven, non-blocking I/O model.
-
Install Node.js
- Download from nodejs.org.
- Check installation:
node -v
,npm -v
.
Hello World in Node.js
console.log("Hello, World!");
Run: node filename.js
.
-
Node.js Modules
- Core modules:
fs
,http
,path
,os
, etc. - Custom modules: Use
module.exports
andrequire()
.
- Core modules:
// math.js
module.exports.add = (a, b) => a + b;
// app.js
const math = require('./math');
console.log(math.add(2, 3)); // 5
-
NPM (Node Package Manager)
- Initialize a project:
npm init
. - Install packages:
npm install package-name
. - Global install:
npm install -g package-name
.
- Initialize a project:
Day 6-10: Core Modules & File System
-
File System (
fs
)- Read a file:
const fs = require('fs'); fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
-
Write to a file:
fs.writeFile('file.txt', 'Hello World', (err) => { if (err) throw err; console.log('File written!'); });
-
HTTP Module
- Create a server:
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!'); }); server.listen(3000, () => console.log('Server running on port 3000'));
-
Path Module
- Join paths:
const path = require('path'); const filePath = path.join(__dirname, 'folder', 'file.txt'); console.log(filePath);
-
OS Module
- Get system info:
const os = require('os'); console.log(os.platform(), os.totalmem(), os.freemem());
Day 11-15: Asynchronous Programming
-
Callbacks
- Example:
function fetchData(callback) { setTimeout(() => callback('Data fetched'), 1000); } fetchData((data) => console.log(data));
-
Promises
- Example:
const fetchData = () => { return new Promise((resolve, reject) => { setTimeout(() => resolve('Data fetched'), 1000); }); }; fetchData().then((data) => console.log(data));
-
Async/Await
- Example:
const fetchData = () => { return new Promise((resolve) => { setTimeout(() => resolve('Data fetched'), 1000); }); }; const getData = async () => { const data = await fetchData(); console.log(data); }; getData();
-
Event Loop
- Understand the event-driven architecture of Node.js.
Day 16-20: Express.js Framework
-
Install Express
-
npm install express
.
-
Basic Server
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello, World!'));
app.listen(3000, () => console.log('Server running on port 3000'));
-
Routing
- Example:
app.get('/about', (req, res) => res.send('About Page')); app.post('/submit', (req, res) => res.send('Form Submitted'));
-
Middleware
- Example:
app.use(express.json()); // Parse JSON bodies app.use((req, res, next) => { console.log('Middleware executed'); next(); });
-
Error Handling
- Example:
app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!'); });
Day 21-25: Database Integration
-
MongoDB with Mongoose
- Install:
npm install mongoose
. - Connect:
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true, useUnifiedTopology: true });
- Install:
Create a Model
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model('User', userSchema);
-
CRUD Operations
- Create:
const user = new User({ name: 'John', age: 25 }); user.save();
-
Read:
User.find({}, (err, users) => console.log(users));
-
Update:
User.updateOne({ name: 'John' }, { age: 30 }, (err) => console.log('Updated'));
-
Delete:
User.deleteOne({ name: 'John' }, (err) => console.log('Deleted'));
Day 26-30: Advanced Topics
-
RESTful APIs
- Design RESTful endpoints for CRUD operations.
-
Authentication
- Use
jsonwebtoken
for JWT-based authentication. - Install:
npm install jsonwebtoken
.
- Use
-
WebSockets
- Use
socket.io
for real-time communication. - Install:
npm install socket.io
.
- Use
-
Deployment
- Deploy Node.js apps to Heroku, Vercel, or AWS.
-
Testing
- Use
Mocha
andChai
for testing. - Install:
npm install mocha chai
.
- Use
Bonus Tips
- Use
nodemon
for auto-restarting the server during development:npm install -g nodemon
. - Use
dotenv
for environment variables:npm install dotenv
. - Explore popular libraries like
axios
,lodash
, andmoment
.
By following this cheat sheet, you'll have a solid understanding of Node.js in 30 days! Happy coding! π
Top comments (0)