Move sendResponse method to a middleware

This commit is contained in:
Daniel García Aubert
2017-11-14 13:49:12 +01:00
parent 520e84e46b
commit a5347c27e3
+15 -12
View File
@@ -18,24 +18,26 @@ AnalysesController.prototype.register = function(app) {
cors(),
userMiddleware,
this.prepareContext,
this.catalog.bind(this)
this.catalog.bind(this),
this.sendResponse()
);
};
AnalysesController.prototype.sendResponse = function(req, res, body) {
res.set('Cache-Control', 'public,max-age=10,must-revalidate');
AnalysesController.prototype.sendResponse = function() {
return function sendResponseMiddleware (req, res) {
res.set('Cache-Control', 'public,max-age=10,must-revalidate');
res.status(200);
res.status(200);
if (req.query && req.query.callback) {
res.jsonp(body);
} else {
res.json(body);
}
if (req.query && req.query.callback) {
res.jsonp(res.body);
} else {
res.json(res.body);
}
};
};
AnalysesController.prototype.catalog = function (req, res, next) {
var self = this;
var username = res.locals.user;
step(
@@ -77,7 +79,7 @@ AnalysesController.prototype.catalog = function (req, res, next) {
return -1;
});
},
function sendResponse(err, catalogWithTables) {
function prepareResponse(err, catalogWithTables) {
if (err) {
if (err.message.match(/permission\sdenied/)) {
err = new Error('Unauthorized');
@@ -86,7 +88,8 @@ AnalysesController.prototype.catalog = function (req, res, next) {
next(err);
} else {
self.sendResponse(req, res, { catalog: catalogWithTables });
res.body = { catalog: catalogWithTables };
next();
}
}
);