├── example ├── router │ ├── index.js │ ├── links.js │ ├── posts │ │ ├── day │ │ │ └── _id.js │ │ ├── month │ │ │ └── _id.js │ │ └── week │ │ │ └── _id.js │ └── users │ │ └── _uid.js └── app.js ├── .gitignore ├── package.json ├── index.js └── readme.md /example/router/index.js: -------------------------------------------------------------------------------- 1 | exports.get = function* () { 2 | this.body = 'Index page'; 3 | } -------------------------------------------------------------------------------- /example/router/links.js: -------------------------------------------------------------------------------- 1 | exports.get = function* () { 2 | this.body = 'This is links' + '\npathRegexp: ' + exports.get.pathRegexp; 3 | } -------------------------------------------------------------------------------- /example/router/posts/day/_id.js: -------------------------------------------------------------------------------- 1 | exports.get = function* (id) { 2 | this.body = 'Day id: ' + id + '\npathRegexp: ' + exports.get.pathRegexp; 3 | }; -------------------------------------------------------------------------------- /example/router/posts/month/_id.js: -------------------------------------------------------------------------------- 1 | exports.get = function* (id) { 2 | this.body = 'Month id: ' + id + '\npathRegexp: ' + exports.get.pathRegexp; 3 | }; -------------------------------------------------------------------------------- /example/router/posts/week/_id.js: -------------------------------------------------------------------------------- 1 | exports.get = function* (id) { 2 | this.body = 'Week id: ' + id + '\npathRegexp: ' + exports.get.pathRegexp; 3 | }; 4 | 5 | exports.week = function* (id) { 6 | this.body = 'Week id: ' + id + '\npathRegexp: ' + exports.get.pathRegexp; 7 | }; -------------------------------------------------------------------------------- /example/router/users/_uid.js: -------------------------------------------------------------------------------- 1 | exports.get = function* (uid, next) { 2 | this.body = 'Users id: ' + uid + '\npathRegexp: ' + exports.post.pathRegexp; 3 | yield* next; 4 | }; 5 | 6 | exports.post = function* (uid) { 7 | this.body = 'Users id: ' + uid + '\npathRegexp: ' + exports.post.pathRegexp; 8 | }; -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | var koa = require('koa'); 2 | var router = require('..'); 3 | 4 | var app = koa(); 5 | 6 | app.use(router(app, { 7 | root: './example/router', 8 | // root: require('path').join(__dirname, 'router'), 9 | wildcard: '_' 10 | })); 11 | 12 | app.listen(3000, function () { 13 | console.log('Listening on 3000!'); 14 | }); -------------------------------------------------------------------------------- /.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 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-frouter", 3 | "version": "0.5.0", 4 | "description": "File/Folder as `path`, another router middleware for koa.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "DEBUG=* node --harmony ./example/app" 8 | }, 9 | "keywords": [ 10 | "koa", 11 | "route", 12 | "router", 13 | "frouter" 14 | ], 15 | "dependencies": { 16 | "koa-rewrite": "https://github.com/nswbmw/rewrite/archive/1.1.1.tar.gz", 17 | "koa-route": "^2.4.0", 18 | "ls-sync": "^0.2.1" 19 | }, 20 | "devDependencies": { 21 | "koa": "*" 22 | }, 23 | "author": "nswbmw", 24 | "license": "MIT", 25 | "repository": "https://github.com/MangroveTech/koa-frouter.git" 26 | } 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var ls = require('ls-sync'); 3 | var route = require('koa-route'); 4 | var rewrite = require('koa-rewrite'); 5 | 6 | module.exports = function (app, options) { 7 | if (typeof options === 'string') { 8 | options = {root: options}; 9 | } else if (!options || !options.root) { 10 | throw new Error('`root` config required.'); 11 | } 12 | var wildcard = options.wildcard || '*'; 13 | var root = options.root; 14 | 15 | //rewrite / to /index 16 | app.use(rewrite('/', '/index')); 17 | 18 | ls(root).forEach(function (filePath) { 19 | if (path.extname(filePath) !== '.js') { 20 | return; 21 | } 22 | var exportFuncs = require(filePath); 23 | var pathRegexp = formatPath(filePath, root, wildcard); 24 | for (var method in exportFuncs) { 25 | try { 26 | exportFuncs[method].pathRegexp = pathRegexp; 27 | app.use(route[method.toLowerCase()](pathRegexp, exportFuncs[method])); 28 | } catch (e) {} 29 | }; 30 | }); 31 | 32 | return function* frouter(next) { 33 | yield* next; 34 | }; 35 | }; 36 | 37 | function formatPath(filePath, root, wildcard) { 38 | return filePath 39 | .replace(path.resolve(process.cwd(), root), '') 40 | .replace(/\\/g, '/') 41 | .replace(new RegExp('/\\' + wildcard, 'g'), '/:') 42 | .split('.')[0]; 43 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## koa-frouter 2 | 3 | File/Folder as `path`, another router middleware for koa. 4 | 5 | ### Install 6 | 7 | npm i koa-frouter --save 8 | 9 | ### Usage 10 | 11 | ``` 12 | router(app, options) 13 | ``` 14 | - app: {Object} koa instance. 15 | - options: {Object|String->root} 16 | - root: {String} router directory 17 | - wildcard: {String} will replace it with ':' 18 | 19 | ### Example 20 | 21 | **File tree** 22 | 23 | ``` 24 | ├── app.js 25 | ├── package.json 26 | ├── ... 27 | └── router 28 | ├── users 29 | │ └── *uid.js 30 | │ 31 | ├── posts 32 | │ ├── month 33 | │ │ └── *id.js 34 | │ ├── week 35 | │ │ └── *id.js 36 | │ └── day 37 | │ └── *id.js 38 | ├── index.js 39 | └── links.js 40 | ``` 41 | 42 | **\*uid.js** 43 | 44 | ``` 45 | exports.post = function* (uid) { ... } 46 | ``` 47 | 48 | **\*id.js** 49 | 50 | ``` 51 | exports.get = function* (id) { ... } 52 | ``` 53 | 54 | **index.js** 55 | 56 | ``` 57 | exports.get = function* () { ... } 58 | ``` 59 | 60 | **links.js** 61 | 62 | ``` 63 | exports.get = function* () { ... } 64 | ``` 65 | 66 | **app.js** 67 | 68 | ``` 69 | var koa = require('koa'); 70 | var router = require('koa-frouter'); 71 | 72 | var app = koa(); 73 | app.use(router(app, { 74 | root: './router' 75 | })); 76 | app.listen(3000); 77 | ``` 78 | equal to: 79 | 80 | ``` 81 | var koa = require('koa'); 82 | var _ = require('koa-route'); 83 | 84 | var users = require('./router/users/*uid.js'); 85 | var month = require('./router/posts/month/*id.js'); 86 | var week = require('./router/posts/week/*id.js'); 87 | var day = require('./router/posts/day/*id.js'); 88 | var links = require('./router/links.js'); 89 | var index = require('./router/index.js'); 90 | 91 | var app = koa(); 92 | 93 | app.use(_.post('/users/:uid', users.post)); 94 | app.use(_.get('/posts/month/:id', month.get)); 95 | app.use(_.get('/posts/week/:id', week.get)); 96 | app.use(_.get('/posts/day/:id', day.get)); 97 | app.use(_.get('/links', links.get)); 98 | app.use(_.get('/', index.get)); 99 | app.use(_.get('/index', index.get)); 100 | 101 | app.listen(3000); 102 | ``` 103 | 104 | ### Test 105 | 106 | npm test 107 | 108 | ### License 109 | 110 | MIT --------------------------------------------------------------------------------