如何绑定表示路由的一些返回Promise的函数?
如何绑定一个返回承诺的函数到一个快速路由?
我有一个函数,与我的模型对象返回承诺,我需要将其添加到控制器function,在特定的快速路线上作出反应 :
app.get('/students/list', controllerFunction);
(我有函数findAllActiveStudents
从文件/数据库读取,并返回承诺)。 如何编写controllerFunction里面调用findAllActiveStudents
并返回结果?
// inside controller file exports controllerFunction = function(req, res) { // Here what to do }
它可以像这样使用:
// inside controller file exports controllerFunction = function(req, res) { var promise = findAllActiveStudents(req); promise.then(function(result) { // format and send the result res.send(result); }, function(err) { // format and send the error res.send(error); }); }