Follow middleware pattern, return a function as the actual middleware

This commit is contained in:
Daniel García Aubert 2018-03-01 15:52:48 +01:00
parent 3caa1d9c4a
commit 2c762813ba
4 changed files with 13 additions and 11 deletions

View File

@ -24,7 +24,7 @@ NamedMapsAdminController.prototype.register = function (app) {
`${base_url_templated}/`,
cors(),
userMiddleware(),
localsMiddleware,
localsMiddleware(),
credentialsMiddleware(),
this.checkContentType('POST', 'POST TEMPLATE'),
this.authorizedByAPIKey('create', 'POST TEMPLATE'),
@ -35,7 +35,7 @@ NamedMapsAdminController.prototype.register = function (app) {
`${base_url_templated}/:template_id`,
cors(),
userMiddleware(),
localsMiddleware,
localsMiddleware(),
credentialsMiddleware(),
this.checkContentType('PUT', 'PUT TEMPLATE'),
this.authorizedByAPIKey('update', 'PUT TEMPLATE'),
@ -46,7 +46,7 @@ NamedMapsAdminController.prototype.register = function (app) {
`${base_url_templated}/:template_id`,
cors(),
userMiddleware(),
localsMiddleware,
localsMiddleware(),
credentialsMiddleware(),
this.authorizedByAPIKey('get', 'GET TEMPLATE'),
this.retrieve()
@ -56,7 +56,7 @@ NamedMapsAdminController.prototype.register = function (app) {
`${base_url_templated}/:template_id`,
cors(),
userMiddleware(),
localsMiddleware,
localsMiddleware(),
credentialsMiddleware(),
this.authorizedByAPIKey('delete', 'DELETE TEMPLATE'),
this.destroy()
@ -66,7 +66,7 @@ NamedMapsAdminController.prototype.register = function (app) {
`${base_url_templated}/`,
cors(),
userMiddleware(),
localsMiddleware,
localsMiddleware(),
credentialsMiddleware(),
this.authorizedByAPIKey('list', 'GET TEMPLATE LIST'),
this.list()

View File

@ -7,7 +7,7 @@ const dbConnSetup = require('./db-conn-setup');
module.exports = function prepareContextMiddleware(authApi, pgConnection) {
return [
locals,
locals(),
cleanUpQueryParams(),
layergroupToken,
credentials(),

View File

@ -1,6 +1,7 @@
module.exports = function localsMiddleware(req, res, next) {
// save req.params in res.locals
module.exports = function locals () {
return function localsMiddleware (req, res, next) {
res.locals = Object.assign(req.params || {}, res.locals);
next();
};
};

View File

@ -65,10 +65,11 @@ describe('prepare-context', function() {
}
it('res.locals are created', function(done) {
const locals = localsMiddleware();
let req = {};
let res = {};
localsMiddleware(prepareRequest(req), prepareResponse(res), function(err) {
locals(prepareRequest(req), prepareResponse(res), function(err) {
if ( err ) { done(err); return; }
assert.ok(res.hasOwnProperty('locals'), 'response has locals');
done();