Code Monkey home page Code Monkey logo

Comments (5)

attilaorosz avatar attilaorosz commented on June 29, 2024

Hi,

I just tested this and in my case both requests trigger someMiddlewareFuction. Which express and routing-controllers versions are you using?

from routing-controllers.

STEVEOO6 avatar STEVEOO6 commented on June 29, 2024

Ahh the plot thickens!

package.json contents

"express": "^4.18.3",
"routing-controllers": "^0.10.4",
"reflect-metadata": "^0.2.1",

package-lock.json contents

"node_modules/express": {
      "version": "4.18.3",
       ...
},
"node_modules/routing-controllers": {
      "version": "0.10.4",
      ...
},
"node_modules/reflect-metadata": {
      "version": "0.2.1",
      ...
}

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 29, 2024

Well, this is a bit frustrating. I'm able to reproduce your issue. The problem is that express actually executes both controllers if you call next() in them. In routing-controllers there was a "fix" for this by setting a param to prevent multiple executions. The reason why it works inconsistently is because of the order of the middlewares/routes registered.
The fixed version doesn't trigger the middleware because after executing it tries to execute the next matching route, the :path one, but because of the "guard" in routing controllers, it stops the flow completely. If you try to execute the :path one, it will not try to execute the fixed one, so there is no stop in the flow, it will go to your outside middleware.

from routing-controllers.

STEVEOO6 avatar STEVEOO6 commented on June 29, 2024

Hmm so if I'm understanding correctly routing-controllers has some built-in mechanism to prevent multiple matching handlers being invoked. But this only stops the control-flow when any route EXCEPT the last one is invoked. Normally this wouldn't be an issue (because the last route doesn't have anything else after it when everything is configured to use routing-controllers) but since I'm using both routing-controllers with some of my own middleware the behaviour is coming to light. Is this roughly with what you're saying?

If so, does that mean if I add a controller function like the following (at the bottom), it would effectively treat the testA/testB endpoints consistently?

@Get('*')
async testC(@Req() req: Request, @Res() res: Response) {
  console.log('i am a special catch-all route')
}

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 29, 2024

It would treat them consistently but also incorrectly. The short circuit was probably never meant to work this way and it was assumed the middlewares would be registered in routing-controllers instead.

from routing-controllers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.