auth0-blog / hapi-jwt-authentication

MIT License
42 stars 14 forks source link

Routes using glob.sync not registering #10

Open camerow opened 7 years ago

camerow commented 7 years ago

Glob.sync is returning an empty array for me. My tree looks the same as the sample:

-- src
    -- api
        -- users
            -- routes
                -- Users.js
-- config.js
-- server.js

and I have

glob.sync('api/**/routes/*.js', {
      root: __dirname
    }).forEach(file => {
      console.log('file --', file);
      const route = require(path.join(__dirname, file));
      server.route(route);
    });

my never runs.

chenkie commented 7 years ago

just to confirm--do you get undefined in your console.log there? or is it just that the routes aren't registering with server.route?

camerow commented 7 years ago

Yes, it was undefined. The solution for me was:

glob.sync('api/**/routes/*.js', {
      cwd: __dirname // root -> cwd
    }).forEach(file => {
      console.log('file --', file);
      const route = require(path.join(__dirname, file));
      server.route(route);
    });
chenkie commented 7 years ago

gotcha, thanks for sharing. which version of glob do you have? and which platform are you running node on?

camerow commented 7 years ago

Platform is Ubuntu 16.04, "glob": "^7.1.1"

I have to say though, I love that implementation for importing routes!

chenkie commented 7 years ago

Cool, it's possible something broke between glob 7.0 and 7.1 but I'm not sure. Would you want to open a PR with the fix?

And yes, using glob for this is much saner :) Things can get out of hand when we start getting a lot of routes.

camerow commented 7 years ago

I can open a PR for sure. I'll to do it later at home though.

lobo commented 7 years ago

@camerow any updates of that PR? :)