How to access node acl across multiple modules?
<p>I am having some trouble understanding how to use the <a href="https://github.com/OptimalBits/node_acl" rel="nofollow">Node ACL</a> with mongoose module. I can get it running fine as long as everything is in one file. However how do I access the acl instance in other modules if I wanted to break up the routes into separate files? </p>

<p>I can get acl working with the following code just fine. It initializes, creates the collections in the database, and adds permissions to the user.</p>

<pre><code>// App.js
const mongoose = require('mongoose');
const node_acl = require('acl');
const User = require('./models/User');

mongoose.connect(/* connection string */);

acl = new node_acl(new node_acl.mongodbBackend(mongoose.connection.db, '_acl'));
acl.allow([
  {
    roles: ['guest'],
    allows: [{ resources: 'login', permissions: 'get' }],
  },
  {
    roles: ['admin'],
    allows: [{ resources: '/users', permissions: '*' }]
  }
]);

var user = User.findOne({username: 'coffee'}, (err, user) =&gt; {
  console.error(user.id);
  acl.addUserRoles(user.id, 'admin');
});
</code></pre>

<p>What I can't figure out is how to properly access the acl instance in another module like this.</p>

<pre><code>// routes/foo.js
const acl = require('acl');
const router = require('express').Router();

// initialize acl ?

router.route('/', acl.middleware(/* rules */), (req, res) =&gt; {
  // route logic
});

module.exports = router;
</code></pre>

<p>This code yields the following error: <code>TypeError: acl.middleware is not a function</code>. </p>

<p>Do I need to create a new instance of acl using the database connection in each route module? If so what is the best way to to get the connection from Mongoose again? If not, or is there a way to pass it to each route?</p>

<p>Thank you!</p>