ExpressJS Crash Course

Jun 22, 2024

ExpressJS Crash Course

Introduction

  • Updated version of the previous course
  • Node.js Crash Course Link provided
  • Node.js is a JavaScript runtime
  • Suggested to watch the Node.js crash course first for a better understanding

Course Outline

  • What is Express.js and its uses
  • Projects: Creating Routes, Request Response Objects, Middleware, Error Handling, and more
  • Mal Utilized by large companies (IBM, Uber, Nike) and start-ups

What is Express.js

  • Minimal Node.js web framework with versatile features for web/mobile apps
  • Used for server-side applications and APIs/microservices
  • Commonly used for backend APIs consumed by frontend frameworks (MERN, MEAN, MEVN, etc.)
  • Flexible and Unopinionated framework: Few rules, more customizability
  • Popular for smaller projects and start-ups
  • Simplifies HTTP request handling

Types of Web Frameworks

  • Opinionated Frameworks:
    • Structured with guided rules/conventions
    • Includes many features out-of-the-box
    • Examples: Ruby on Rails, Django, Laravel, NestJS (Node.js)
  • Unopinionated Frameworks:
    • Flexible with minimal rules
    • Fewer features out-of-the-box, but highly customizable
    • Examples: Express.js

Prerequisites for Learning ExpressJS

  • Strong understanding of JavaScript (including ES6 features like arrow functions, destructuring, async/await)
  • Basic Node.js knowledge (recommended 2-hour crash course)
  • Familiarity with npm and handling JSON data

Course Practical Sections

  1. Server Setup: Creating a package.json and installing Express
  2. Setting Up Basic Server with routes
    • Using app.listen, app.get for starting the server and handling requests
    • Using Nodemon or --watch flag for auto-restart
  3. Serving Static HTML Files
    • Using middleware app.use(express.static())
  4. Handling JSON APIs
    • Setting up API routes to send JSON data
    • Using Postman for testing different HTTP methods
  5. Working with Environment Variables
    • Creating and using a .env file without a third-party package
  6. Handling Dynamic Routes and Params
    • Using req.params and req.query to handle route and query parameters
  7. Managing HTTP Status Codes
    • Properly sending statuses like 200, 404, 500 for different scenarios
  8. Optimizing Route File Structure
    • Moving route definitions to separate files and using Express Router
  9. Handling Request Body Data
    • Parsing JSON and form-data using body-parser middleware adapted within Express
  10. CRUD Operations
    • Implementing CRUD endpoints for posts
    • Using basic error handling and custom messages

Middleware in Express

  • Definition and Examples
    • Creating a logger middleware and using it at route and application levels
    • Importing third-party libraries (colors to add color to console logs)

Custom Error Handling

  • Using custom middleware for error handling
  • Handling different types of errors (404, 500, etc.) using the error object in middleware

Controllers

  • Moving logic to controllers for cleaner code
  • Importing and using controller functions in route files

Frontend Interaction Example

  • Setting up a Static Folder and Loading HTML Files
    • Using vanilla JavaScript to interact with Express API
    • Creating forms and using the Fetch API to send/receive data

Template Engines (Quick Overview)

  • Using ejs Template Engine
    • Setting up ejs in Express and rendering dynamic HTML
    • Passing variables and arrays from routes to views
    • Using partials and layouts for repeated sections (like headers)

Conclusion

  • Encouragement to keep practicing and understanding will solidify over time.

Additional Resources