From b10937a8fb8c90cb0ebe5e3b20580e6d964ef8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 24 Nov 2017 15:49:25 +0100 Subject: [PATCH] Use res.locals instead of req.context to share object among middlewares --- app/controllers/job_controller.js | 10 +++++----- app/middlewares/authenticated-request.js | 4 ++-- app/middlewares/user.js | 2 +- app/server.js | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/controllers/job_controller.js b/app/controllers/job_controller.js index bc06128f..4b13b1bc 100644 --- a/app/controllers/job_controller.js +++ b/app/controllers/job_controller.js @@ -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 })); diff --git a/app/middlewares/authenticated-request.js b/app/middlewares/authenticated-request.js index d7ddffec..a06ddeea 100644 --- a/app/middlewares/authenticated-request.js +++ b/app/middlewares/authenticated-request.js @@ -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); }); diff --git a/app/middlewares/user.js b/app/middlewares/user.js index 57e9d8b5..0c6cdbd3 100644 --- a/app/middlewares/user.js +++ b/app/middlewares/user.js @@ -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(); }; diff --git a/app/server.js b/app/server.js index 4f4ecbbc..5d0ad9a0 100644 --- a/app/server.js +++ b/app/server.js @@ -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);