How to find out which express handler function will be called next?

Viewed 332

Is there a way to check what is next middleware function in call stack in express application?

I've tried to check by comparing next function with the reference to function i surely know will be called next.

public static async requestMapper(request: Request, response: Response, next: Function){
    console.log(next == ExampleController.exampleHandler)
    next();
}

Even though I indeed know that ExampleController.exampleHandler is called next, output of this snippet is false

1 Answers

From express documentation it's not possible to access to next middleware. Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

It's same for nest.js

I think you have bad approach if you try to understand which next middleware.

Related