📚

Comprehensive Node.js and Express.js Tutorial

May 5, 2025

Node.js and Express.js Tutorial Series

Introduction

  • The series consists of 15 tutorials focused on Node.js, Express.js, and MongoDB.
  • Recommended for those familiar with JavaScript basics.
  • Projects include building a web server and handling user authentication.

Chapter 1: Introduction to Node.js

  • Node.js: JavaScript runtime built on Chrome's V8 engine.
  • Server-side JavaScript: Unlike client-side JS with HTML/CSS, Node.js runs on the server.
  • Installation: Download from nodejs.org.
  • Editor: Use Visual Studio Code for code examples.
  • Terminal: Node.js console is in the terminal, not browser dev tools.

Differences from Vanilla JavaScript

  • Global Object: Node.js uses global instead of window.
  • Common Core Modules: Access to OS, file systems, etc., using require.
  • Module Syntax: Uses CommonJS require vs. ES6 import.
  • APIs: Some browser APIs are missing, e.g., fetch.

File System Module

  • Reading/Writing Files: Using fs module to handle file operations.
  • Async Nature: Node.js handles operations asynchronously.
  • Error Handling: Catch uncaught exceptions.
  • FS Promises: Use async/await for cleaner code.

Streams and Directories

  • Streams: Efficiently handle large files using fs.createReadStream and createWriteStream.
  • Directories: Use fs.mkdir and fs.rmdir to manage directories.

Node Package Manager (npm)

  • Installation: npm i package-name, manage dependencies in package.json.
  • Scripts: Define start, dev, and build scripts for automation.
  • Semantic Versioning: Understanding major, minor, patch updates.

Working with Events

  • Event Emitter: Create and listen for events with the events module.
  • Logging: Use custom logging with timestamps and IDs.

Creating a Web Server

  • HTTP Module: Use http to create a server.
  • Routing: Handle different URL routes using switch statements initially.
  • File Serving: Serve static files, handle 404s.

Express.js Framework

  • Express Installation: npm i express.
  • Routing with Express: Simplifies routing with methods like app.get().
  • Middleware: Built-in, custom, and third-party middleware.

Middleware in Express

  • Built-in Middleware: For handling JSON, serving static files, etc.
  • Custom Middleware: Create logging and error handling.
  • Third-party Middleware: Use cors for cross-origin requests.

Routing with Express

  • Express Router: Organize routes using routers for different endpoints.
  • Route Handlers: Chain multiple handlers per route using middleware.

MongoDB and Mongoose

  • MongoDB Introduction: NoSQL database storing data in collections/documents.
  • Mongoose: An ODM for MongoDB in Node.js.
  • Schemas and Models: Define schemas to represent data structures.

CRUD Operations with Mongoose

  • Connecting to MongoDB: Use mongoose.connect with connection URI.
  • Defining Schemas: Set up schemas for users and employees.
  • Performing CRUD: Implement CRUD operations using Mongoose methods.

User Authentication and JWT

  • User Registration: Securely store user data with bcrypt for passwords.
  • JWT: Issue JSON Web Tokens for authentication and authorization.
  • Protected Routes: Use middleware to safeguard routes based on JWT.

Implementing Authorization

  • Roles and Permissions: Add roles to users for access control.
  • Verify Roles Middleware: Check user roles before allowing access.

Deployment

  • Deploying to Glitch: Use the Glitch platform to host your Node.js application.
  • Environment Variables: Use .env to manage secrets like tokens and URI.

Conclusion

  • A comprehensive tutorial series covering Node.js, Express.js, and MongoDB.
  • Ideal for building a full REST API with authentication and authorization.
  • Recommended next steps: Explore the MERN stack by adding React.