mansona / express-autoroute

Helper module to automatically load Express routes from a structured routes/ folder
MIT License
35 stars 10 forks source link

Absolute path's needed #14

Open crobinson42 opened 8 years ago

crobinson42 commented 8 years ago

Great module, guys! I see a big problem with the paths and how they're being passed around. It would be great if the directory path (absolute path) was parsed right away, then all the following logic down the call chain for directory & file paths are failproof.

I tried using this with the following directory structure and it fails:

app.js 
routes/
    user.js
config/
    routes.js

I place the logic which requires and initializes express-autoroute inside config/routes.js and set the option routesDir: 'routes'. After throwing a few console logs in the source code for express-autoroute I quickly realized the paths are all screwed up. I'm willing to put in a PR for this fix that will use the root directory (where the node app was started) as the relative path starting point for reference.

Here's the error I'm seeing:

error: Error autoloading routes.  error=Cannot find module 'routes/user.js', file=routes/user.js
error: Error: Cannot find module 'routes/user.js'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at loadRouteObj (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:19:20)
    at loadFile (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:88:13)
    at /Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:70:13
    at Array.forEach (native)
    at Object.loadDirectory (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:62:11)
    at module.exports (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/index.js:24:16)
error: Error loading file: routes/user.js
error: Error: Error autoloading routes
    at loadFile (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:97:23)
    at /Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:70:13
    at Array.forEach (native)
    at Object.loadDirectory (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:62:11)
    at module.exports (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/index.js:24:16)
    at module.exports.app (/Users/willlyrob/projects/api-server/routes.js:20:3)
    at Object.<anonymous> (/Users/willlyrob/projects/api-server/app_compiled.js:26:20)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:106
            throw new Error("Error loading file: " + file);
            ^

Error: Error loading file: routes/user.js
    at loadFile (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:106:19)
    at /Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:70:13
    at Array.forEach (native)
    at Object.loadDirectory (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/lib/loader.js:62:11)
    at module.exports (/Users/willlyrob/projects/api-server/node_modules/express-autoroute/index.js:24:16)
    at module.exports.app (/Users/willlyrob/projects/api-server/routes.js:20:3)
    at Object.<anonymous> (/Users/willlyrob/projects/api-server/app_compiled.js:26:20)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
crobinson42 commented 8 years ago

@mansona here's an example of how you'd implement this:

https://github.com/saikojosh/Express-Route-Builder/blob/master/index.js#L89