Use res.locals instead of req.context to share object among middlewares

This commit is contained in:
Daniel García Aubert 2017-11-24 15:49:25 +01:00
parent c2d1a4653e
commit b10937a8fb
4 changed files with 9 additions and 9 deletions

View File

@ -78,9 +78,9 @@ JobController.prototype.createJob = function (req, res) {
var sql = (params.query === "" || _.isUndefined(params.query)) ? null : params.query;
var data = {
user: req.context.user,
user: res.locals.user,
query: sql,
host: req.context.userDbParams.host
host: res.locals.userDbParams.host
};
this.jobService.create(data, jobResponse(req, res, this.statsdClient, 'create', 201));
@ -105,7 +105,7 @@ JobController.prototype.listWorkInProgressJobs = function (req, res) {
if (process.env.NODE_ENV !== 'test') {
console.info(JSON.stringify({
type: 'sql_api_batch_job',
username: req.context.user,
username: res.locals.user,
action: 'list'
}));
}
@ -123,7 +123,7 @@ function jobResponse(req, res, statsdClient, action, status) {
return handleException(err, res);
}
res.header('X-Served-By-DB-Host', req.context.userDbParams.host);
res.header('X-Served-By-DB-Host', res.locals.userDbParams.host);
req.profiler.done(action);
req.profiler.end();
@ -135,7 +135,7 @@ function jobResponse(req, res, statsdClient, action, status) {
if (process.env.NODE_ENV !== 'test') {
console.info(JSON.stringify({
type: 'sql_api_batch_job',
username: req.context.user,
username: res.locals.user,
action: action,
job_id: job.job_id
}));

View File

@ -14,7 +14,7 @@ function authenticatedMiddleware(userDatabaseService) {
var params = _.extend({}, req.query, body);
var authApi = new AuthApi(req, params);
userDatabaseService.getConnectionParams(authApi, req.context.user, function cancelJob(err, userDbParams) {
userDatabaseService.getConnectionParams(authApi, res.locals.user, function cancelJob(err, userDbParams) {
req.profiler.done('setDBAuth');
if (err) {
@ -25,7 +25,7 @@ function authenticatedMiddleware(userDatabaseService) {
return handleException(new Error('permission denied'), res);
}
req.context.userDbParams = userDbParams;
res.locals.userDbParams = userDbParams;
return next(null);
});

View File

@ -2,6 +2,6 @@ var CdbRequest = require('../models/cartodb_request');
var cdbRequest = new CdbRequest();
module.exports = function userMiddleware(req, res, next) {
req.context.user = cdbRequest.userByReq(req);
res.locals.user = cdbRequest.userByReq(req);
next();
};

View File

@ -104,7 +104,7 @@ function App(statsClient) {
// Use step-profiler
app.use(function bootstrap$prepareRequestResponse(req, res, next) {
req.context = req.context || {};
res.locals = res.locals || {};
if (global.settings.api_hostname) {
res.header('X-Served-By-Host', global.settings.api_hostname);