Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inversify-express-utils] Nesting controllers and maintaining route context #371

Open
Cyberuben opened this issue Nov 20, 2019 · 2 comments

Comments

@Cyberuben
Copy link

I'm just getting into InversifyJS and I'm struggling to get my head around the concept of Express controllers.

Usually I write my code in such a fashion that the routing chain is executed step by step and the context of the request is incrementally stored inside the request object.

routes/index.ts

router.use("/users", usersRouter);

export default router;

routes/users/index.ts

router.use("/:userId", (req, res, next) => {
  let user = fakedSyncGetUserById(req.params.userId);

  req.context.user = user;

  next();
});

router.use("/:userId/posts", postsRouter);

router.get("/:userId", (req, res) => {
  res.status(200).json(req.context.user);
});

routes/users/posts.ts

router.get("/", (req, res) => {
  let posts = fakeGetPostsByUser(req.context.user);

  res.status(200).json(posts);
});

This way I don't have to validate the user in the controller that manages the posts. How would this be achieved in a sane way using InversifyJS?

Another issue I couldn't find the answer to is the order routes are executed, such /users/me being executed before /users/:userId is evaluated.

@PodaruDragos PodaruDragos transferred this issue from inversify/InversifyJS Jan 28, 2022
@andreidiaconescu
Copy link

andreidiaconescu commented Oct 13, 2022

Hello @PodaruDragos @Cyberuben
did you find the answer ? what is the order in which routes are run in the controller ?

thank you !

@PodaruDragos
Copy link
Member

PodaruDragos commented Oct 13, 2022

hello @andreidiaconescu.
I did not actually investigated this issue.
I had moved these issues here because I wanted to focus on solving them, but I didn't really had any time on my plate.

inversify-express-utils does not really do anything to the order, it's just express under.

I'll reopen this just so it's clear that it's not solved.

@PodaruDragos PodaruDragos reopened this Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants