The declaration order of middleware and routes determines the execution order: Express walks through them in order. The body parser must come before the routes that use it, the 404 route comes after all routes, and the error handler comes last.
Example
app.use(express.json());
app.use('/api', routes);
app.use(notFound);
app.use(errorHandler);