diff --git a/docs/server.md b/docs/server.md index b827ed1..77d3a5d 100644 --- a/docs/server.md +++ b/docs/server.md @@ -140,7 +140,14 @@ functions in the form `f(req, res, next)` or arrays of functions of the same signature (ldapjs will unroll them). Unlike HTTP, LDAP operations do not have a heterogeneous wire format, so each -operation requires specific methods/fields on the request/response objects. +operation requires specific methods/fields on the request/response +objects. However, there is a `.use()` method availabe, similar to +that on express/connect, allowing you to chain up "middleware": + + server.use(function(req, res, next) { + console.log('hello world'); + return next(); + }); ## Common Request Elements diff --git a/lib/server.js b/lib/server.js index d0eecb1..c916e8e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -271,6 +271,7 @@ function Server(options) { EventEmitter.call(this, options); + this._chain = []; this.log4js = options.log4js; this.log = this.log4js.getLogger('Server'); @@ -617,6 +618,16 @@ Server.prototype.unbind = function() { }; +Server.prototype.use = function use() { + var args = Array.prototype.slice.call(arguments); + var chain = mergeFunctionArgs(args, 0, args.length); + var self = this; + chain.forEach(function(c) { + self._chain.push(c); + }); +}; + + Server.prototype.after = function() { if (!this._postChain) this._postChain = []; @@ -792,7 +803,12 @@ Server.prototype._mount = function(op, name, argv, notDN) { index = 1; } var route = this._getRoute(notDN ? name : dn.parse(name), backend); - route['0x' + op.toString(16)] = mergeFunctionArgs(argv.slice(index)); + + var chain = this._chain.slice(); + argv.slice(index).forEach(function(a) { + chain.push(a); + }); + route['0x' + op.toString(16)] = mergeFunctionArgs(chain); return this; };