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?
如果您有一个快速应用程序并定义模型,它是否成为MVC应用程序,还是更有必要?
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 个解决方案
#1
10
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.
首先,express中的路由是connect中定义的中间件。 express和其他框架之间的区别在于中间件大多位于控制器前面,控制器结束响应。 Express使用中间件的另一个原因是Node.js的性质是异步的。
Lets see what a controller might look like in Javascript.
让我们看看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.
你可能会注意到这个get动作的第一件事是嵌套回调。这很难测试,难以阅读,如果你需要编辑东西,你需要弄乱你的缩进。因此,我们通过使用流量控制并使其平坦来解决这个问题。
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.
因此,虽然技术上可能在express.js中有控制器,但您可能会*使用流控制结构,这最终与中间件相同。
#1
10
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.
首先,express中的路由是connect中定义的中间件。 express和其他框架之间的区别在于中间件大多位于控制器前面,控制器结束响应。 Express使用中间件的另一个原因是Node.js的性质是异步的。
Lets see what a controller might look like in Javascript.
让我们看看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.
你可能会注意到这个get动作的第一件事是嵌套回调。这很难测试,难以阅读,如果你需要编辑东西,你需要弄乱你的缩进。因此,我们通过使用流量控制并使其平坦来解决这个问题。
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.
因此,虽然技术上可能在express.js中有控制器,但您可能会*使用流控制结构,这最终与中间件相同。