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

Using the same controller for multiple requests #17

Open
move-zig opened this issue May 26, 2021 · 0 comments
Open

Using the same controller for multiple requests #17

move-zig opened this issue May 26, 2021 · 0 comments

Comments

@move-zig
Copy link

move-zig commented May 26, 2021

It seems like you create a singleton for each controller and use a controller's execute method as a handler for express routes. BaseController.execute is synchronous, but it calls the sub-class's executeImpl method, which might be asynchronous. executeImpl might call BaseController.conflict, BaseController.fail, etc. which rely on BaseController.req and BaseController.res.

If multiple requests to the same endpoint come in faster than they can be handled, the multiple calls to execute will overwrite the req and res fields. Then when the executeImpl method finally calls this.conflict, won't it be using the wrong res for res.send?

Instead, should we not create a new controller each time we want to handle a route?

E.g.,

- import { createUserController } from '../../../useCases/createUser';
+ import { CreateUserController } from '../../../useCases/createUser/CreateUserController';
+ import { createUserUseCase } from '../../../useCases/createUser'; // export the use case singleton in this file

const userRouter = express.Router();

userRouter.post('/',
-   (req, res) => createUserController.execute(req, res)
+   (req, res) => {
+     const controller = new CreateUserController(createUserUseCase);
+     controller.execute(req, res);
+   }
)
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

1 participant