vendredi 11 décembre 2015

Should my controller take the (res, req) params or already extracted params?

I wonder what way should I organize my routing in expressJS :

Params parsing in Controller

router.get('/users/:id', UserController.get);

class UserController {
  get(res, req) {
    var id = res.params.id;
    UserModel.get(id, function(user) {
      res.send(user);
    }
  }
}

Params parsing in Route

router.get('/users/:id', function(req, res) {
  var id = req.params.id;
  UserController.get(id, function(user) {
    res.json(user);
  }
});

class UserController {
  get(id, fn) {
    UserModel.get(id, fn);
  }
}

I find the second version Params parsing in Route easier to unit test, but most of the example I found use the first version, why ?

Aucun commentaire:

Enregistrer un commentaire