Reputation: 25
So we are trying to build a website and we have different routes but they have somewhat same functioning
So say:
for should i make separate controller for each route or should i try to combine the few of them?
and what might be the reason to choose one above another?
// I have a controller
const getAllProducts = () =>{} // for both fruits and vegetables but it feels messy to use
const getProduct = () => {} // to get a single product (both fruits and veg)..
const getProductsRecomended = () => {} // to get the recomended products (both fruits and veg)..
// tho i have these controller it's confusing to manage the logic and the routes properly
Upvotes: 0
Views: 625
Reputation: 1
A controller works in terms of resources, in your case I would say the resource would be product.
From what I understand, the rest of the route would be comprised of route parameters, where fruits & veg are the type of product, and you have an optional id in there.
Within the product controller you would have 3 methods & 3 routes to accompany them:
You don't want separate controllers for different actions, that's what different routes & handlers/methods within a controller are for. You want a separate controller ifit is controlling a different resource.
As you are using express, these would have routes defined like so:
app.get('/products/:productType/rec', (req, res) => {
...
})
app.get('/products/:productType/', (req, res) => {
...
})
app.get('/products/:productType/:id', (req, res) => {
...
})
See this page of the express docs to get more details on how to use these route parameters: https://expressjs.com/en/guide/routing.html
The purpose behind this is to group these routes & methods together, as they are all working with products.
However, there's a separate question about your abstraction - are fruits and veg:
product
class, but there is a property, such as productType
that distinguishes between themproduct
classFrom what you have described, it sounds like they should be one of the last 2, but if they are entirely different classes, without many shared properties, you will probably need 2 separate controllers to handle this, as otherwise the logic behind these route handlers can get messy. It will also allow you more granular control over what you fetch from the API.
Upvotes: 0
Reputation: 1
You definitely shouldn't create a controller for each route. one way of doing it is to have 3 controllers: 1. for rec products 2. for all products 3. for individual product. But on the other hand we would be repeating quite a bit of code and upsetting DRY principals, since you would basically be doing the same thing(retrieving veg/fruit). it really depends on the size of the app and how large it will grow. if it's a small app, the first way is okay, if its a big project like lets say an ecommerce website for large chain, I would use the second way.
first way:
in controller number 1, i would have 2 .get requests, 1 for veg 1 for fruit, with different routes: /fruit, /veg.
in controller 2, 1 get request for all products
in controller 3, again 2 requests 1 for veg - /veg/:id, 1 for fruit /fruit/:id.
second way:
have one controller with different routes, the requests would be the same as in the first way, everything would just be in one controller with name(for example) getFruitsAndVegetables.js
Upvotes: 0