Mikey
Mikey

Reputation: 6766

TypeError: acl.middleware() is not a function

I am trying to use ACL in my Node.js application.

Using acl module, I have the following code:

var express = require('express'), 
    logger = require('morgan'),
    mongoose = require('mongoose'),
    Acl = require('acl');

var app = express();

app.set('port', 8000);

mongoose.connect('mongodb://localhost:27017/test', function () {   
    console.log('Mongodb connection connected');

    var acl = new Acl(new Acl.mongodbBackend(mongoose.connection.db, 'acl_'), logger());

    acl.allow([
        {
            roles: ['guest'],
            allows: [
                { resources: '/test', permissions: 'get' }
            ],
        }
    ]);

    console.log('acl.middleware is a ', typeof acl.middleware);

    app.get('/test', acl.middlemare(), function (req, res) {

    });

    app.get('/unauthorized', acl.middlemare(), function (req, res) {

    });
});

app.listen(app.get('port'), function () {
    console.log('Express server listening on port %d', app.get('port'));
});

When I start the server, I get the following output:

Express server listening on port 8000
Mongodb connection connected
acl.middleware is a function
TypeError: acl.middleware() is not a function
...

Edit: After placing a logger, I am a given: Error: Broke parameter contract

I cannot figure out what I am doing wrong.

I have looked at this related answer but still have no luck at getting it to work.

Is there something else that I am missing?

Upvotes: 0

Views: 831

Answers (2)

niceseb
niceseb

Reputation: 523

You have to use Promise to delay the instantiation of ACL until Express has fully initialized.

isMongoDBConnected = new Promise((resolve, reject) => {
    MongoClient.connect(mongoDBUrl, (e, db) => {
        if (e) {
            reject(e);
        } else {

            var mongoBackend = new NodeAcl.mongodbBackend(db /*, {String} prefix */);
            acl = new NodeAcl(mongoBackend, logger);

            defineRoles(acl);
            resolve(true);
        }
    });
});

Upvotes: 0

mscdex
mscdex

Reputation: 106736

It's acl.middleware() not acl.middlemare() (w vs. m).

Upvotes: 1

Related Questions