But I'm getting has no method 'middleware'
error
That's because your Kc
object doesn't have a middleware
property. It has a prototype
property that has middleware
.
If Kc
is a function, you probably wanted to use it like this:
var Kc = require('connect-kc');
var kc = new Kc();
server.use(kc.middleware());
If Kc
isn't a function, then:
var Kc = require('connect-kc');
server.use(Kc.prototype.middleware());
...but I would strongly recommend you not give it a property called prototype
, as that's very misleading. The prototype
property on function instances references the object that that function will assign to instances when instances are created by new TheFunctionName
.
Side note 1:
This code is suspect:
options.logout = options.logout || '/logout';
options.admin = options.admin || '/';
It's generally not a good idea to reach out and change the caller's object like that. Instead, typically it's useful to have a function around that copies properties between instances (it's frequently called extend
), and then:
var opts = extend({}, defaults, options);
...where defaults
has those default logout
and admin
options. Then use opts
rather than options
. The extend
function looks something vaguely like this:
function extend(target) {
Array.prototype.slice.call(arguments, 1).forEach(function(arg) {
Object.keys(arg).forEach(function(key) {
target[key] = arg[key];
});
});
return target;
}
Side note 2:
You can create the middlewares
array much more succinctly and (subjectively) clearly if you like:
var middlewares = [
Setup,
PostAuth(this),
AdminLogout(this, options.admin),
GrantAttacher(this),
Logout(this, options.logout)
];