Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from leaplegal/master
Browse files Browse the repository at this point in the history
Dynamic routes are being loaded before static routes
  • Loading branch information
jsdevel committed Apr 11, 2016
2 parents d1e68f4 + 4f7bc89 commit 7924884
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,37 @@ var memo = {};

module.exports = fsRoutes;

function compare(a, b) {
var result;
a = {
dirname: path.dirname(a).replace(/^\./i, ''),
basename: path.basename(a).replace(/\:/i, '~')
};
b = {
dirname: path.dirname(b).replace(/^\./i, ''),
basename: path.basename(b).replace(/\:/i, '~')
};

if(a.dirname === b.dirname) {
result = -1;
if(a.basename > b.basename) {
result = 1;
}
} else {
result = 1;
if(a.dirname < b.dirname) {
result = -1;
}
}

return result
}

function fsRoutes(dir) {
dir = path.resolve(process.cwd(), dir);

if (!memo[dir]) {
memo[dir] = glob.sync('**/*.js', {cwd: dir}).map(function(file) {
memo[dir] = glob.sync('**/*.js', {cwd: dir}).sort(compare).map(function(file) {
return {
path: path.resolve(dir, file),
route: '/' + file.replace(/\.js$/, '')
Expand Down
Empty file added test-dir/users/query.js
Empty file.
Empty file added test-dir/users/{id}.js
Empty file.
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ function assertRoutes(routes) {
path: __dirname + '/test-dir/home.js',
route: '/home'
},
{
path: __dirname + '/test-dir/users/query.js',
route: '/users/query'
},
{
path: __dirname + '/test-dir/users/{id}.js',
route: '/users/{id}'
},
{
path: __dirname + '/test-dir/users/:id.js',
route: '/users/:id'
Expand Down

0 comments on commit 7924884

Please sign in to comment.