node.jsexpressurl-routingurl-design

What is key factor for deciding routes vs parameters


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

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?


Solution

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