0

So we are trying to build a website and we have different routes but they have somewhat same functioning

So say:

  1. for recomended products
    localhost:3000/products/veg/rec
    localhost:3000/products/fruits/rec
  2. for viewing all products
    localhost:3000/products/veg
    localhost:3000/products/fruits
  3. for viewing single product
    localhost:3000/products/veg/:id
    localhost:3000/products/fruits/:id

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
delirium78
  • 612
  • 4
  • 13
Aditya
  • 1
  • 2

1 Answers1

0

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

zukach
  • 1
  • 1