What is the difference between a node.js express route and a controller?

谁说胖子不能爱 提交于 2020-01-01 08:12:20

问题


Is there anything that is different or more powerful with a traditional controller over an express route?

If you have an express app and define models, does it become an MVC application, or is there more necessary?

I'm just wondering if I'm missing out on extra/easier functionality in my node express apps by not upgrading to a more legitimate 'controller'. If there is such a thing.

Thanks!

Edit: To clarify, if you use a route like so:

// routes/index.js
exports.module = function(req, res) {
  // Get info from models here, 
  res.render('view', info: models);
}

What makes it any different from a controller? Is a controller able to do more?


回答1:


First of all a route in express is middleware as defined in connect. The difference between express and other frameworks is that middleware mostly sits in front of the controller and the controller ends the response. An other reason why express uses middleware is due to the nature of Node.js being asynchronous.

Lets see what a controller might look like in Javascript.

var Controller = function () { };

Controller.prototype.get = function (req, res) {

  find(req.param.id, function (product) {

    res.locals.product = product;

    find(res.session.user, function (user) {

      res.locals.user = user;
      res.render('product');

    }); 

  }); 

};  

The first thing you probably notice about this get action is the nested callbacks. This is hard to test, hard to read and if you need to edit stuff you need to fiddle with your indentation. So lets fix this by using flow control and make it flat.

var Controller = function () { };

Controller.prototype.update = function (req, res) {

  var stack = [

    function (callback) {

      find(req.param.id, function (product) {
        res.locals.product = product;
        callback();
      });


    },

    function (callback) {

      find(res.session.user, function (user) {
        res.locals.user = user;
        callback();
      });

    }

  ];

  control_flow(stack, function (err, result) {
    res.render('product');
  });

}

In this example you can extract all the different functions of the stack and test them or even re-use them for different routes. You might have noticed that the control flow structure looks a lot like middleware. So lets replace the stack with middleware in our route.

app.get('/',

  function (req, res, next) {

    find(req.param.id, function (product) {
      res.locals.product = product;
      next();
    });

  },

  function (req, res, next) {

    find(res.session.user, function (user) {
      res.locals.user = user;
      next();
    });

  },

  function (req, res, next) {
    res.render('product');
  }

);

So while it might technically be possible to have controllers in express.js you would probably be forced to use flow control structures, which in the end is the same as middleware.



来源:https://stackoverflow.com/questions/12943553/what-is-the-difference-between-a-node-js-express-route-and-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!