DEV Community

Abdul Haseeb
Abdul Haseeb

Posted on

30-Day Node.js Crash Course Cheat Sheet

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

  1. What is Node.js?

    • JavaScript runtime built on Chrome's V8 engine.
    • Event-driven, non-blocking I/O model.
  2. Install Node.js

    • Download from nodejs.org.
    • Check installation: node -v, npm -v.
  3. Hello World in Node.js

   console.log("Hello, World!");
Enter fullscreen mode Exit fullscreen mode

Run: node filename.js.

  1. Node.js Modules
    • Core modules: fs, http, path, os, etc.
    • Custom modules: Use module.exports and require().
   // math.js
   module.exports.add = (a, b) => a + b;

   // app.js
   const math = require('./math');
   console.log(math.add(2, 3)); // 5
Enter fullscreen mode Exit fullscreen mode
  1. NPM (Node Package Manager)
    • Initialize a project: npm init.
    • Install packages: npm install package-name.
    • Global install: npm install -g package-name.

Day 6-10: Core Modules & File System

  1. 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!');
     });
    
  1. 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'));
    
  2. Path Module

    • Join paths:
     const path = require('path');
     const filePath = path.join(__dirname, 'folder', 'file.txt');
     console.log(filePath);
    
  3. OS Module

    • Get system info:
     const os = require('os');
     console.log(os.platform(), os.totalmem(), os.freemem());
    

Day 11-15: Asynchronous Programming

  1. Callbacks

    • Example:
     function fetchData(callback) {
       setTimeout(() => callback('Data fetched'), 1000);
     }
     fetchData((data) => console.log(data));
    
  2. Promises

    • Example:
     const fetchData = () => {
       return new Promise((resolve, reject) => {
         setTimeout(() => resolve('Data fetched'), 1000);
       });
     };
     fetchData().then((data) => console.log(data));
    
  3. 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();
    
  4. Event Loop

    • Understand the event-driven architecture of Node.js.

Day 16-20: Express.js Framework

  1. Install Express

    • npm install express.
  2. 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'));
Enter fullscreen mode Exit fullscreen mode
  1. Routing

    • Example:
     app.get('/about', (req, res) => res.send('About Page'));
     app.post('/submit', (req, res) => res.send('Form Submitted'));
    
  2. Middleware

    • Example:
     app.use(express.json()); // Parse JSON bodies
     app.use((req, res, next) => {
       console.log('Middleware executed');
       next();
     });
    
  3. Error Handling

    • Example:
     app.use((err, req, res, next) => {
       console.error(err.stack);
       res.status(500).send('Something broke!');
     });
    

Day 21-25: Database Integration

  1. MongoDB with Mongoose

    • Install: npm install mongoose.
    • Connect:
     const mongoose = require('mongoose');
     mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true, useUnifiedTopology: true });
    
  2. Create a Model

   const userSchema = new mongoose.Schema({
     name: String,
     age: Number,
   });
   const User = mongoose.model('User', userSchema);
Enter fullscreen mode Exit fullscreen mode
  1. 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

  1. RESTful APIs

    • Design RESTful endpoints for CRUD operations.
  2. Authentication

    • Use jsonwebtoken for JWT-based authentication.
    • Install: npm install jsonwebtoken.
  3. WebSockets

    • Use socket.io for real-time communication.
    • Install: npm install socket.io.
  4. Deployment

    • Deploy Node.js apps to Heroku, Vercel, or AWS.
  5. Testing

    • Use Mocha and Chai for testing.
    • Install: npm install mocha chai.

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, and moment.

By following this cheat sheet, you'll have a solid understanding of Node.js in 30 days! Happy coding! πŸš€

Top comments (0)