├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json ├── routes ├── api │ ├── index.js │ └── user.js ├── home.js ├── movies.js └── users.js ├── routes2 ├── api │ ├── index.js │ └── user.js ├── home2.js ├── movies2.js └── users2.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 alfred sang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mount-routes 2 | 3 | mount-routes = auto mount express routes with routes_folder_path 4 | 5 | [![gitter][gitter-image]][gitter-url] 6 | [![NPM version][npm-image]][npm-url] 7 | 8 | ## Install 9 | 10 | npm install --save mount-routes 11 | 12 | ## Usages 13 | 14 | 15 | ``` 16 | var express = require('express') 17 | var app = express() 18 | 19 | var mount = require('mount-routes'); 20 | 21 | // simple 22 | // mount(app); 23 | // with path 24 | // mount(app, __dirname + '/routes2'); 25 | 26 | // with path & api dump 27 | mount(app, __dirname + '/routes2', true); 28 | 29 | // start server 30 | app.listen(23018) 31 | ``` 32 | 33 | ## Contributing 34 | 35 | 1. Fork it 36 | 2. Create your feature branch (`git checkout -b my-new-feature`) 37 | 3. Commit your changes (`git commit -am 'Add some feature'`) 38 | 4. Push to the branch (`git push origin my-new-feature`) 39 | 5. Create new Pull Request 40 | 41 | 42 | ## 版本历史 43 | 44 | - v1.0.3 add api dump feature 45 | - v1.0.2 rename index to / 46 | - v1.0.0 初始化版本 47 | 48 | 49 | ## 欢迎fork和反馈 50 | 51 | - write by `i5ting` shiren1118@126.com 52 | 53 | 如有建议或意见,请在issue提问或邮件 54 | 55 | ## License 56 | 57 | this repo is released under the [MIT 58 | License](http://www.opensource.org/licenses/MIT). 59 | 60 | 61 | [npm-image]: https://img.shields.io/npm/v/mount-routes.svg?style=flat-square 62 | [npm-url]: https://npmjs.org/package/mount-routes 63 | [gitter-image]: https://badges.gitter.im/Join%20Chat.svg 64 | [gitter-url]: https://gitter.im/i5ting/mount-routes?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var path = require('path'); 3 | var requireDirectory = require('require-directory'); 4 | var routes = requireDirectory(module, './routes'); 5 | 6 | var stack = []; 7 | /** 8 | * Mount routes with directory. 9 | * 10 | * Examples: 11 | * 12 | * // mount routes in app.js 13 | * require('./config/routes')(app); 14 | * 15 | * @param {Object} app 16 | * @param {Object} routes 17 | * @param {String} pre 18 | * @return 19 | * @api public 20 | */ 21 | function mount(app) { 22 | var r = arguments[1] || routes; 23 | var pre = arguments[2] || ''; 24 | 25 | for (var k in r) { 26 | var file = '/' + pre + '' + k + '.js'; 27 | // console.log('mount route ' + file + " "); 28 | var path = ''; 29 | if(typeof r[k] == 'object') { 30 | // console.log('this is a obj'); 31 | mount(app, r[k], pre + k + '/'); 32 | }else if(k === 'index') { 33 | path = '/'+ pre; 34 | _use(app, file, path, r[k]); 35 | }else { 36 | path = '/' + pre + '' + k; 37 | _use(app, file, path, r[k]); 38 | } 39 | } 40 | } 41 | 42 | function _use(app, file, path, handler) { 43 | // console.dir(handler) 44 | // console.log(handler.stack) 45 | app.use(path, handler); 46 | 47 | _track_routes(file, path, handler.stack); 48 | } 49 | 50 | function _track_routes(file, path, handle) { 51 | for(var i in handle){ 52 | var _route = handle[i].route; 53 | // console.log(_route); 54 | // console.log(_route.stack); 55 | // console.log(_route.methods); 56 | if(!_route)continue; //添加了非空的处理逻辑,挂载中间件的路由不予显示在路由列表中 57 | 58 | var params = _route.stack.params; 59 | 60 | for(var j in _route.methods){ 61 | if(_route.path == '/'){ 62 | _cache_to_stack(file, path, j); 63 | }else{ 64 | _cache_to_stack(file, path + _route.path, j); 65 | } 66 | } 67 | } 68 | } 69 | 70 | function _cache_to_stack(file, path, method) { 71 | // console.log(file+ ' ' +method + ' ' + path) 72 | stack.push({ 73 | file : file, 74 | method : method, 75 | path : path 76 | }); 77 | } 78 | 79 | function _dump(routes_folder_path) { 80 | var Table = require('cli-table'); 81 | var table = new Table({ head: ["File", "Method", "Path"] }); 82 | 83 | // console.log(stack) 84 | console.log('\n******************************************************'); 85 | console.log('\t\tMoaJS Apis Dump'); 86 | console.log('******************************************************\n'); 87 | 88 | for (var k in stack) { 89 | var obj = stack[k]; 90 | // console.dir(k) 91 | // console.log(obj.file + obj.method + obj.path) 92 | table.push( 93 | [routes_folder_path + obj.file, obj.method, obj.path] 94 | ); 95 | } 96 | 97 | console.log(table.toString()); 98 | } 99 | 100 | /** 101 | * Mount routes with directory. 102 | * 103 | * Examples: 104 | * 105 | * // mount routes in app.js 106 | * mount(app, 'routes2', true); 107 | * 108 | * @param {Object} app 109 | * @param {String} routes_folder_path 110 | * @param {Boolean} is_debug 111 | * @return 112 | * @api public 113 | */ 114 | function mount_with_folder(app, routes_folder_path) { 115 | stack = [];// empty when enter 116 | 117 | var r = arguments[1] || './routes'; 118 | var is_debug = arguments[2] || false; 119 | 120 | r = path.resolve(path.dirname(require.main.filename), r) 121 | 122 | // console.log('mount routes_folder_path = ' + r) 123 | routes = requireDirectory(module, r); 124 | 125 | mount(app) ; 126 | 127 | if(is_debug){ 128 | _dump (routes_folder_path); 129 | } 130 | } 131 | 132 | module.exports = mount_with_folder; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mount-routes", 3 | "version": "1.0.7", 4 | "description": "mount-routes = auto mount express routes with routes_folder_path", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "npm publish .", 8 | "test": "node test.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/i5ting/mount-routes.git" 13 | }, 14 | "keywords": [ 15 | "express", 16 | "routes", 17 | "route", 18 | "mount", 19 | "require", 20 | "directory" 21 | ], 22 | "author": "i5ting", 23 | "license": "ISC", 24 | "bugs": { 25 | "url": "https://github.com/i5ting/mount-routes/issues" 26 | }, 27 | "homepage": "https://github.com/i5ting/mount-routes#readme", 28 | "dependencies": { 29 | "cli-table": "^0.3.1", 30 | "debug": "^2.2.0", 31 | "require-directory": "^2.1.1" 32 | }, 33 | "devDependencies": { 34 | "express": "^4.12.4" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /routes/api/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | // res.render('index', { title: 'Express' }); 7 | res.status(200).json({ 8 | data:[{'title':'api/'}], 9 | status:{ 10 | code: 0, 11 | msg : 'success' 12 | } 13 | }) 14 | }); 15 | 16 | module.exports = router; 17 | -------------------------------------------------------------------------------- /routes/api/user.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.status(200).json({ 7 | data:[], 8 | status:{ 9 | code: 0, 10 | msg : 'success' 11 | } 12 | }) 13 | }); 14 | 15 | router.get('/2', function(req, res, next) { 16 | res.status(200).json({ 17 | data:[], 18 | status:{ 19 | code: 0, 20 | msg : 'success' 21 | } 22 | }) 23 | }); 24 | 25 | module.exports = router; 26 | -------------------------------------------------------------------------------- /routes/home.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.render('index', { title: 'Express' }); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /routes/movies.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | //configure routes 4 | 5 | var router = express.Router(); 6 | 7 | router.route('/movies') 8 | .get(function(req,res){ 9 | 10 | }) 11 | 12 | .post(function(req,res){ 13 | 14 | }); 15 | 16 | router.route('/movies/:id') 17 | .put(function(req,res){ 18 | 19 | }) 20 | 21 | .get(function(req,res){ 22 | 23 | }) 24 | 25 | .delete(function(req,res){ 26 | 27 | }); 28 | 29 | module.exports=router; 30 | -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res, next) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /routes2/api/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | // res.render('index', { title: 'Express' }); 7 | res.status(200).json({ 8 | data:[{'title':'route2/api/'}], 9 | status:{ 10 | code: 0, 11 | msg : 'success' 12 | } 13 | }) 14 | }); 15 | 16 | module.exports = router; 17 | -------------------------------------------------------------------------------- /routes2/api/user.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.status(200).json({ 7 | data:[], 8 | status:{ 9 | code: 0, 10 | msg : 'success' 11 | } 12 | }) 13 | }); 14 | 15 | router.get('/2', function(req, res, next) { 16 | res.status(200).json({ 17 | data:[], 18 | status:{ 19 | code: 0, 20 | msg : 'success' 21 | } 22 | }) 23 | }); 24 | 25 | module.exports = router; 26 | -------------------------------------------------------------------------------- /routes2/home2.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.render('index', { title: 'Express' }); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /routes2/movies2.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | //configure routes 4 | 5 | var router = express.Router(); 6 | 7 | router.route('/movies') 8 | .get(function get_all_movies(req,res){ 9 | 10 | }) 11 | 12 | .post(function(req,res){ 13 | 14 | }); 15 | 16 | router.route('/movies/:id') 17 | .put(function(req,res){ 18 | 19 | }) 20 | 21 | .get(function(req,res){ 22 | 23 | }) 24 | 25 | .delete(function(req,res){ 26 | 27 | }); 28 | 29 | module.exports=router; 30 | -------------------------------------------------------------------------------- /routes2/users2.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res, next) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | // for test 2 | var express = require('express') 3 | var app = express() 4 | 5 | var mount = require('./index'); 6 | 7 | // simple 8 | // mount(app); 9 | 10 | // with path 11 | mount(app, 'routes2', true); 12 | 13 | // start server 14 | app.listen(23018) 15 | 16 | --------------------------------------------------------------------------------