K
K
komigor2021-07-09 09:39:59
Node.js
komigor, 2021-07-09 09:39:59

How to properly describe controllers?

I need to implement two routes and two controllers for them
1) POST /upload/dog/image:
And the second one.
2) GET /list/dog/images
What I'm going to do is to create an Images controller class and define two methods dogList and dogUpload in it. But the question is whether this is correct from an architectural point of view, because if the system expands, then we will have to write the same for cats and dogs, etc., and if there are 100 of these species, then it turns out that for each one we will have to write a separate method, for example, list and upload . Tell me how to implement this more elegantly and universally in Node.js, and even better using typescript.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Den4eg37, 2021-07-10
@Den4eg37

In my opinion, it is necessary to completely revise the structure of the routes.
dog || cat || monkey is basically a parameter, I would use query parameters, and process 1 route /images, something like
router.post('/images', imagesControler.uploadImages)
router.get('/images', imagesControler.getImages)
A in the controller from request.query, you can set the limits for pagination and more freedom for scaling, and it will not be a problem to implement it at the front

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question