Getting error CreateListFromArrayLike called on non-object when trying to use .apply()
<p>I've created a simple little route parsing function so that I can keep my code clean and easily maintainable, this is the little function that gets ran when the app starts up and parses the <code>config.json</code> file and binds the appropriate methods and request paths:</p>

<pre><code>const fs = require('fs');
const path = require('path');
module.exports = function(app, root) {
  fs.readdirSync(root).forEach((file) =&gt; {
    let dir = path.resolve(root, file);
    let stats = fs.lstatSync(dir);
    if (stats.isDirectory()) {
      let conf = require(dir + '/config.json');
      conf.name = file;
      conf.directory = dir;
      if (conf.routes) route(app, conf);
    }
  })
}

function route(app, conf) {
  let mod = require(conf.directory);

  for (let key in conf.routes) {
    let prop = conf.routes[key];
    let method = key.split(' ')[0];
    let path = key.split(' ')[1];

    let fn = mod[prop];
    if (!fn) throw new Error(conf.name + ': exports.' + prop + ' is not defined');
    if (Array.isArray(fn)) {
      app[method.toLowerCase()].apply(this, path, fn);
    } else {
      app[method.toLowerCase()](path, fn);
    }
  }
}
</code></pre>

<p>The problem I have is some times I need to pass in multiple arguments to an express router, e.g. in the case of using passport something like this:</p>

<pre><code>exports.authSteam = [
  passport.authenticate('facebook', { failureRedirect: '/' }),
  function(req, res) {
    res.redirect("/");
  }
];
</code></pre>

<p>So I figure I can just pass them as an array, and then parse them into the router appropriately, my config for example looks like this:</p>

<pre><code>{
  "name": "Routes for the authentication",
  "description": "Handles the authentication",
  "routes": {
    "GET /auth/facebook": "authFacebook",
    "GET /auth/facebook/return": "authFacebookReturn"
  }
}
</code></pre>

<p>The only problem is I'm getting this error:</p>

<pre><code>     app[method.toLowerCase()].apply(this, path, fn);
                                ^

TypeError: CreateListFromArrayLike called on non-object
</code></pre>

<p>if I <code>console.log(fn)</code> I see <code>[ [Function: authenticate], [Function] ]</code></p>

<p>I'm not exactly sure what I am doing wrong, any information would be great thanks.</p>