Middlewares, Global Catches & Zod

In this lecture, Harkirat dives deep into M**iddlewares:** behind-the-scenes helpers that tidy up things before your main code does its thing. G**lobal catches:** safety nets for your code, they catch unexpected issues before they cause chaos. And finally, Zod: a library that ensures efficient input validation on your behalf.

Untitled

Understanding Middlewares:

Imagine a Busy Hospital:

Think of a hospital where there's a doctor, patients waiting in line, and a few helpful assistants making sure everything runs smoothly.

  1. Doctor's Cabin (Application Logic) :
  2. Waiting Room (Callback Queue) :
  3. Intermediates (Middlewares) :

Untitled

Middlewares in JS Context & Problem Statement:

Earlier we used to organize all our prechecks followed by the application logic all in one route.

Middlewares emerged as a solution to enhance code organization by extracting prechecks from the core application logic. The motivation behind their introduction lies in our commitment to the "Don't Repeat Yourself" (DRY) principle.

By isolating these preliminary checks into distinct functions or code blocks known as middlewares, we achieve a more modular and maintainable codebase. This separation not only streamlines the primary application logic but also promotes code reuse, making it easier to manage, understand, and scale our software architecture.

Untitled