DEV Community

Devmancer
Devmancer

Posted on

my understanding of crud applications,controllers, and middlewares.

Okay, before we start - quick disclaimer: I am in no way a professional. I'm just typing this stuff here because I'm too lazy to write notes in my book.

Now, let's begin!

To me, CRUD applications feel like the backbone of many systems. They help us Create, Read, Update, and Delete data - you know, the stuff everyone knows. But during my adventure in the world of CRUD, I seemed to have found another layer, a deeper mystery so to speak.

Long story short, I basically had to understand the difference between middleware and controllers. To be honest, I don't really know where to draw the line completely, but my knowledge has evolved since last time - I'll tell you that!

I used to think that anything with app.use() was middleware (not my proudest moment, to be honest - don't judge! Also, if you're planning to, please notice the #newbie hashtag - thanks!).

My Middleware vs Controller Revelation
Now I've figured out it's much more intriguing than that. The main distinction I found separating middleware and controllers is that controllers handle the final response, while middleware uses next() to pass control to the next function.

Code exmaple:

code example

just think of middlewares as waiters:
they,

Grinds the beans

Steams the milk

Checks if you paid

Then passes it along "Next!"

then controllers they are the ones who:

Puts everything together

Adds the final touches

Hands you the finished drink "Here's your coffee!"

(beautiful illustration analogy btw )

The Order Matters Saga
Oh, and lest I forget - did you know that the order of middleware actually matters? Funny story: I actually found that out while setting up routes for my operations, and I put updateUser before validateUser. Thank god for ChatGPT, am I right guys?

Anyway, let's not glaze Sam right now. I'm quite happy I've gained this knowledge!

Follow me to see my next blog on Swagger documentation!

Top comments (0)