Move sendResponse method to a middleware
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user