0

I have below code that I want to import in Node modules:

Kc.prototype.middleware = function(options) {

  options.logout = options.logout || '/logout';
  options.admin  = options.admin  || '/';

  var middlewares = [];

  middlewares.push( Setup );
  middlewares.push( PostAuth(this) );
  middlewares.push( AdminLogout(this, options.admin) );
  middlewares.push( GrantAttacher(this) );
  middlewares.push( Logout(this, options.logout) );

  return middlewares;
};

I'm requiring this in my main.js:

var kc = require('connect-kc');

server.use(kc.middleware());

But I'm getting has no method 'middleware' error

What can I do to require and use middleware in my main.js?

I'm using Restify framework.

Passionate Engineer
  • 10,034
  • 26
  • 96
  • 168
  • 1
    On a side note, don't use push for static arrays, the array literal syntax is much nicer. `[Setup, PostAuth(this), ...]` and to answer your question we would have to see the exports of the connect-kc module. – plalx Feb 01 '15 at 17:31
  • 1
    What is `Kc`? How/what do you export in that `connect-kc` module? – Bergi Feb 01 '15 at 17:32

1 Answers1

1

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)
];
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • How can I change it so I can use `middleware` without instantiating it and just use `require`? – Passionate Engineer Feb 01 '15 at 17:51
  • @PassionateDeveloper: Put `middleware` right on your `Kc` object, rather than on an object referenced from a property called `prototype`. – T.J. Crowder Feb 01 '15 at 17:53
  • Great it works thank you. I'm also getting ` options.logout = options.logout || '/logout'; ^ TypeError: Cannot read property 'logout' of undefined` I thought this is optional but apparently it works when I omit `()` in `server.use(Kc.middleware)` – Passionate Engineer Feb 01 '15 at 17:56