Middleware in Express

noun

say it: “MID-ul-wair in ek-SPRES” /ˈmɪdəlwɛr ɪn ɪkˈsprɛs/

A function that runs on a request before the route, such as logging or checking a token, and calls next() to pass it on.

What is Middleware in Express?

Middleware is a function that runs for requests before the route handler, like logging or checking a token, and calls next() to continue.

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

In a sentence

  • “The logging middleware writes the method and URL of every request.”
  • “An auth middleware answers 401 or calls next() when the token is good.”

Level: Apprentice · More Node.js terms at this level

Updated