Giter Club home page Giter Club logo

Comments (15)

calvintwr avatar calvintwr commented on July 19, 2024 3

I didn't set up acl. However I realised that my app.locals example is incorrect.

Can you do this instead?:

Set up acl modularly:

// apps/acl.js
const ACL2 = require("acl2");// same as acl, just maintained
const acl = new ACL2(new ACL2.memoryBackend()); // bladybla, it will show user not authenticated

module.exports = acl
// routes/testacl.is
const router = require('express').Router();
const acl = require('../apps/acl');

router.get('/', acl.middleware(), (req, res, next) => {
    res.send("hello world");
});

module.exports = router;

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024 1

No worries mate easy.

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

@tutyamxx Internally, what it does is to go through your directories and do all the requires.

Does your code work when you require the routings via the standard express way?

Perhaps you can share a larger chunk of your code with me?

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

The code works the standard way, but using the routes inside the app.js where I declared it. See the docs for ACL here. While using express-routemagic, It can't find it, for some reason. Unfortunately, I can't share the large chunk of code, but hopefully if you check the url I mentioned, it might make some sense?

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

Ok let me get to it. I’ll update you.

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

@tutyamxx I have verified that middlewares will continue to work.

I suspect what happened is that you didn't declare acl in your routings, nor separate them into their own routing files to begin with. If this is the error here, you have to re-declare acl. See as follows:

Everything inside app.js

When you do a router.get inside of app.js, acl is within scope:

// app.js
const acl = new ACL()
router.get('/testacl'. acl.middleware(), (req, res, next) => { })

Re-declaring modules when separating routing code from app.js

But acl is not within scope of your routing files if you separate them. You need to re-declare them in each of the routing files:

//testacl.js
const acl = require('somewhere/where/ACL/is/initialised')
const router = require('express').Router()

router.get('/', (req, res, next) => {})

module.exports = router
// app.js
app.use('/testacl', require('./routes/testacl'))

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

thank you for the information, not sure what I did wrong but IM GLAD IT WORKS, definitely trying to see the error on my side...

EDIT: You meant

// app.js
const acl = new ACL();
//testacl.js
const acl = require('app.js') // initialized here
const router = require('express').Router()

router.get('/', acl.middleware(), (req, res, next) => {})

module.exports = router

rather than

//testacl.js
const acl = require('app.js') // initialized here
const router = require('express').Router()

router.get('/', (req, res, next) => {})

module.exports = router

?

I'm slightly confused

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

@tutyamxx yup that’s right. Just redeclare acl in your routing files and you will be alright.

alternatively, if you need it for all your routes, do:

//app.js
const acl = new ACL() //initialise it correctly.

app.use(acl.middleware()) // this has to be above #magic.use, so that all requests gos through this before the routings.

magic.use(app)

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

I only use it in some specific endpoints. My vscode doesn't even find the autocompletion for middleware() in the routings :-s

I get TypeError: acl.middleware is not a function using the sample provided by you

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

Here, a sample to test.

// app.js

const express = require('express')
const app = express()
const port = 3000
const routesMagic = require('express-routemagic');
const ACL2 = require("acl2");// same as acl, just maintained
const acl = new ACL2(new ACL2.memoryBackend()); // bladybla, it will show user not authenticated

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

// below, works fine as you said
//app.get('/testacl', acl.middleware(), (req, res, next) => { })

// --| Enable automatic routing for everything in the routes folder
routesMagic.use(app, {
    routesFolder: 'routes',
    invokerPath: __dirname,
    allowSameName: true,
    ignoreSuffix: ['_bak', '_old', '_dev']
});

module.exports = app;

but in routes/testacl.js

//testacl.js
const acl = require('../app.js') // initialized above in app.js
const router = require('express').Router()

router.get('/', acl.middleware(), (req, res, next) => {
    res.send("hello world");
});

module.exports = router;

^ throws

router.get('/', acl.middleware(), (req, res, next) => {
                    ^

TypeError: acl.middleware is not a function

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

@tutyamxx thanks for the code.

It is what it says. acl is out of scope because you didn’t provide it. Doing:

const acl = require('../app.js')

Doesn’t put acl in scope. That require gives you the express app instance, and not acl. You can console log to see that.

This is an express thing. There’s 2 ways use a middleware.

  1. If everything or a lot of your routes needs to push through acl:
// routes that don’t need to pass through acl should be in separate folder
// if all your routes need acl, omit this.
magic.use(app, {
    routesFolder: 'routes_no_acl'
})

// now use acl to protect whatever that’s below it
app.use(acl.middleware())

// and everything below here has passed through acl
magic.use(app) // this will require the routes in the 'routes' folder by default

Watch out for folder path collision between the two. If you define the same paths in the two, the one below will overwrite. It’s just how express works.

  1. Only few routes need acl:
    Use app.locals to pass acl into the scope of the routes
// app.js
const app = express()
app.locals = acl

magic.use(app)
// routes/testacl.is
const app = require('../app.js') // initialized above in app.js
const router = require('express').Router()

router.get('/', app.locals.acl.middleware(), (req, res, next) => {
    res.send("hello world");
});

module.exports = router;

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

Thanks for the explanation, very kind of you, however second method you provided does throw again saying TypeError: Cannot read properties of undefined (reading 'acl'). I'm losing my bloody mind 😂

from express-routemagic.

calvintwr avatar calvintwr commented on July 19, 2024

Add me on skype calvin.twr

I’ll fix it for you

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

Unfortunately I don't have skype. I need to understand why is it not working lol, because I've used your second version (did you test it?)

After more digging, why it doesn't work, i tried to console.log app.locals in testacl.js and i got (node:5692) Warning: Accessing non-existent property 'locals' of module exports inside circular dependency

Here's my code, nothing more, nothing less.

//app.js

const express = require('express')
const app = express()
const port = 3000
const routesMagic = require('express-routemagic');

const Acl = require('acl2');
const acl = new Acl(new Acl.memoryBackend()); // bladybla, it will show user not authenticated
app.locals = acl;

// --| Enable automatic routing for everything in the routes folder
routesMagic.use(app, {
    routesFolder: 'routes',
    invokerPath: __dirname,
    allowSameName: true,
    ignoreSuffix: ['_bak', '_old', '_dev']
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

module.exports = app;

and

//routes/testacl.js
const app = require('../app.js') // initialized here
const router = require('express').Router();

router.get('/', app.locals.acl.middleware(), (req, res, next) => {
    res.send("hello world");
});

module.exports = router;

now, try to run it on your machine. Perhaps it's a OS pathing again?

from express-routemagic.

tutyamxx avatar tutyamxx commented on July 19, 2024

Works, thanks for the support kind human!

from express-routemagic.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.