Question

I can't see a basis to decide between two factorings of routes. My question is what factor is key to deciding between:

  • more routes - fewer parameters
  • fewer routes - more parameters - logic in handler

The two samples below are my actual case, but I think this is a general question.

My baseline is 2 routes each with 2 parameters:

URI's:
/poweronoff?id=4&val=1
/powerset?id=7&val=75

// app.js
app.get('/poweronoff', mymodule.poweronoff);
app.get('/powerset', mymodule.powerset);

// mymodule
exports.poweronoff= function(req, res){
   setonoff(req.query.id, req.query.val, req, res);
   }

exports.powerset = function(req, res){
   setvalue(req.query.id, req.query.val, req, res);
   }

The alternate factoring is 1 route with 3 parameters.

URI's:
/power?action=onoff&id=4&val=1
/power?action=set&id=7&val=75

// app.js
app.get('/power', mymodule.power);

// mymodule
exports.power = function(req, res){
  if (req.query.action = 'onoff') {
    setonoff(req.query.id, req.query.val, req, res);
  }
  else {
    setvalue(req.query.id, req.query.val, req, res);
  }
}

These two seem nearly equal to me. The difference is one branch within the routing table, versus one if branch in mymodule. Is there caching, memoization, or other factors that tilt the balance in favor of one of these factorings? Does the client side contribute a factor?

Was it helpful?

Solution

This is rather a philosophical question which doesn't have the right answer.

  • If you implement a REST service you might want to use RESTful URI design.
  • If you are free to choose but you like clean design you might stick to clean URL design.
  • If you simply want to learn general principles for good URL design, you might want to check this great answer.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top