Menu

1 / 2

What Is Middleware in Express and How It Works

DEV Community·Pratham·6 months ago
#2eiXTmPv
#example#part#middleware#next#express
Reading 0:00
15s threshold

The checkpoint system between request and response — where logging, authentication, and validation live.

When I first saw this code in Express, I had no idea what it was doing:

app . use (( req , res , next ) => { console . log ( ` ${ req . method } ${ req . url } ` ); next (); });

Enter fullscreen mode

Exit fullscreen mode

It's not a route. It doesn't send a response. And what's next() ? Why do we need to call it? What happens if we don't?

This, I learned, is middleware — and it's the most powerful concept in Express. Every request that hits your server passes through middleware before it reaches your route handler. It's where you log requests, check authentication, validate data, parse bodies, and handle errors — all without cluttering your route logic.

Understanding middleware was the moment Express went from "a routing library" to "a complete application framework" in my mind.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.