inversify / inversify-express-utils

Some utilities for the development of Express application with InversifyJS
MIT License
579 stars 98 forks source link

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

Open Cyberuben opened 4 years ago

Cyberuben commented 4 years ago

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.

andreidiaconescu commented 1 year ago

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

thank you !

PodaruDragos commented 1 year ago

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.