├── .gitignore ├── README.md ├── index.js ├── lib └── express-walker.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | npm-debug.log* 3 | *Thumbs.db -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express Walker 2 | This is a helper module to allow easily loading express routers for APIs. Routes are determined by the directory structure and filenames you use. Also, it allows the passing of parameters in any fashion you wish to each router as its loaded, allowing the easy passing of models and other variables throughout your project. 3 | 4 | ## Installation 5 | ```sh 6 | npm install express-walker 7 | ``` 8 | 9 | ## Example 10 | 11 | ### Your index.js 12 | ```js 13 | var app = require('express')(); 14 | var walker = new (require('express-walker'))( 15 | { 16 | directory: './api/v0.1/', 17 | app: app, 18 | root: '/api/' 19 | } 20 | ) 21 | .pass(app, models, yetAnotherArgument) 22 | .load() 23 | .then(function(){ 24 | //All routers have been loaded into the express app now. 25 | app.listen(3000); 26 | }); 27 | ``` 28 | 29 | ### Your directory structure: 30 | 31 | ``` 32 | | 33 | |--api 34 | |----v0.1 35 | |------admin 36 | |--------users.js 37 | |--------sales.js 38 | |------register.js 39 | |------doStuff.js 40 | ``` 41 | 42 | ### Your typical router 43 | _doStuff.js_ 44 | ```js 45 | var express = require('express'); 46 | 47 | module.exports = function(app, models, yetAnotherArgument){ 48 | 49 | var doStuffRouter = express.Router(); 50 | 51 | doStuffRouter.route('/') 52 | .get(function(req, res){ 53 | console.log("Hello World!"); 54 | }); 55 | 56 | return doStuffRouter 57 | }; 58 | ``` 59 | _Note the return_ 60 | 61 | ### And, finally, your resulting API paths: 62 | 63 | ```no-highlight 64 | /api/admin/users 65 | /api/admin/sales 66 | /api/register/ 67 | /api/doStuff/ 68 | ``` 69 | 70 | ## Documentation 71 | 72 | ### Setup 73 | 74 | ```js 75 | var walker = new (require('express-walker'))(); 76 | walker 77 | .directory('./targetDir') 78 | .root('targetAPIRoot') 79 | .app(app) 80 | .pass(list, of, arguments, to, pass) 81 | .load() 82 | .then(function(){ 83 | app.listen(3000); 84 | }); 85 | 86 | //OR 87 | 88 | var walker = new (require('express-walker'))( 89 | { 90 | app: app, 91 | directory: './targetDir', 92 | root: 'targetAPIRoot', 93 | args: [list, of, arguments, to, pass] 94 | } 95 | ) 96 | .load(function(){ 97 | app.listen(3000); 98 | }); 99 | ``` 100 | 101 | ### Parameters 102 | 103 | #### Directory 104 | The directory you wish to walk through. All sub-directories are also traversed. 105 | 106 | #### Root 107 | The root of the API to be added as ap refix to each router when its added. If a router had an endpoint of '/test/something' A root of '/' would not add anything as a prefix - the resulting route would be '/test/something'. A root of '/api/' would result in an endpoint of '/api/test/something'. 108 | 109 | #### app 110 | The express app you've created - it's used to add each router to the app. 111 | 112 | #### args 113 | Optional. When added in the constructor, it's an array of values or variables. when using the .pass() method, it's called with as many arguments as you wish (without the array). You can have as many or as little as you wish. 114 | 115 | These arguments are passed, in the same order, to each router on require-time. 116 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = require('./lib/express-walker.js')(); -------------------------------------------------------------------------------- /lib/express-walker.js: -------------------------------------------------------------------------------- 1 | var express = require('express'), 2 | walk = require('walk').walk, 3 | Promise = require('promise'), 4 | path = require('path'); 5 | 6 | module.exports = function(){ 7 | 8 | var ExpressWalker = function(opts){ 9 | if(typeof opts == 'object'){ 10 | this.app = opts.app; 11 | this.directory = opts.directory; 12 | this.root = opts.root; 13 | this.args = opts.args; 14 | } 15 | }; 16 | 17 | ExpressWalker.prototype.pass = function(){ 18 | this.args = Array.prototype.slice.call(arguments); 19 | return this; 20 | }; 21 | 22 | ExpressWalker.prototype.app = function(app){ 23 | this.app = app; 24 | return this; 25 | }; 26 | 27 | ExpressWalker.prototype.directory = function(dir){ 28 | this.directory = dir; 29 | return this; 30 | }; 31 | 32 | ExpressWalker.prototype.root = function(root){ 33 | this.root = root; 34 | return this; 35 | } 36 | 37 | ExpressWalker.prototype.load = function(cb){ 38 | var self = this; 39 | 40 | return new Promise(function(resolve, reject){ 41 | 42 | var walker = walk(self.directory); 43 | 44 | walker.on('file', function(root, fileStat, next){ 45 | //Make sure we only act on javascript files 46 | if(fileStat.name.length > 3 && 47 | fileStat.name.substr(fileStat.name.length - 3).toLowerCase() != '.js') 48 | return next(); 49 | 50 | var url = root.replace(self.directory, '').replace('\\', '/'); 51 | if(url == ''){ 52 | url = self.root + '/' + fileStat.name.split('.js')[0]; 53 | } else { 54 | url = self.root + '/' + url + '/' + fileStat.name.split('.js')[0] 55 | } 56 | url = url.replace(/\/\/+/g, '/').toLowerCase(); 57 | 58 | self.app.use(url, 59 | require(path.resolve(root) + path.sep + fileStat.name).apply( 60 | null, 61 | self.args 62 | ) 63 | ); 64 | next(); 65 | }); 66 | 67 | walker.on('end', function(){ 68 | if(cb) cb(); 69 | resolve(); 70 | }); 71 | }); 72 | 73 | }; 74 | 75 | return ExpressWalker; 76 | 77 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-walker", 3 | "version": "1.0.1", 4 | "description": "A router loader for express apps.", 5 | "main": "index.js", 6 | "dependencies": { 7 | "express": "^4.12.4", 8 | "walk": "^2.3.9", 9 | "promise": "^7.0.1" 10 | }, 11 | "devDependencies": {}, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/hlfshell/express-walker.git" 18 | }, 19 | "keywords": [ 20 | "router", 21 | "express", 22 | "directory" 23 | ], 24 | "author": "Keith Chester ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/hlfshell/express-walker/issues" 28 | }, 29 | "homepage": "https://github.com/hlfshell/express-walker" 30 | } 31 | --------------------------------------------------------------------------------