Node.js / Express.js - middleware calls order

0 points
Created by:

In this article, we would like to show you middleware calls order in Express.js.

The order is one of the most important things about middleware. The order in which they are written or included in the file is the order in which they are executed.

Practical example

In this example, we specify the middleware functions before the request in the order we want them to be called.

const express = require('express');
const app = express();

// first middleware before the response is sent
app.use((req, res, next) => {
  console.log('Before response');

// second middleware after the response is sent
app.use('/', (req, res) => {
  console.log('After response');

// GET request (route handler)
app.get('/', (req, res, next) => {


Now when you send a request to the http://localhost:3000/ you should see the following result in the console:

Before response
After response
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂