'Does the position of error handling middleware matter in express?

I was just curious since I thought it wouldn't matter where you place the middleware in your code as long as it's in the app/index.js

What I mean is this:

const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");

const app = express();
dotenv.config();

// MIDDLEWARE
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use(cors());

// ROUTES
const { errorHandler } = require("./middleware/errorMiddleware");
const goalRoutes = require("./routes/goalRoutes");

app.use("/api/goals", goalRoutes);

app.use(errorHandler);

If I put the errorHandler middleware above the routes like this:

// MIDDLEWARE
const { errorHandler } = require("./middleware/errorMiddleware");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.use(cors());
app.use(errorHandler);

// ROUTES

const goalRoutes = require("./routes/goalRoutes");
app.use("/api/goals", goalRoutes);

The errorHandler wouldn't work anymore. Does the position matter for this kind of middleware? What other types of middleware needs to be in a proper position?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source