@@ -2,23 +2,23 @@ const path = require('path')
2
2
const fs = require ( 'fs' )
3
3
const qs = require ( 'querystring' )
4
4
5
- // create global paramPattern but define it in the router to allow custom char for parameterized routes
6
- let paramPattern
5
+
6
+ const paramPattern = / : ( [ ^ \/ % ] + ) /
7
7
8
8
// takes routes and decorates them with a 'match' method that will return { params, query } if a path matches
9
- function addMatch ( route ) {
9
+ function addMatch ( route ) {
10
10
let routePath = route . path
11
11
let paramNames = [ ]
12
12
let matched
13
13
// find any paths prefixed with a `:`, and treat them as capture groups
14
- while ( matched = routePath . match ( paramPattern ) ) {
15
- routePath = routePath . replace ( paramPattern , '([^\?\ /]+)' )
14
+ while ( ( matched = routePath . match ( paramPattern ) ) !== null ) {
15
+ routePath = routePath . replace ( paramPattern , '([^? /]+)' )
16
16
paramNames . push ( matched [ 1 ] )
17
17
}
18
18
// if a route ends with `index`, allow matching that route without matching the `index` part
19
19
if ( path . basename ( routePath ) === 'index' ) {
20
20
route . isIndex = true
21
- routePath = routePath . replace ( / \/ i n d e x $ / , '\ /?(:?index)?' )
21
+ routePath = routePath . replace ( / \/ i n d e x $ / , '/?(:?index)?' )
22
22
}
23
23
// create a regex with our path
24
24
let pattern = new RegExp ( `^${ routePath } (\\?(.*)|$)` , 'i' )
@@ -38,7 +38,7 @@ function addMatch(route) {
38
38
}
39
39
40
40
// recursively searches for all js files inside a directory tree, and returns their full paths
41
- function findRoutes ( dir ) {
41
+ function findRoutes ( dir ) {
42
42
let files = fs . readdirSync ( dir )
43
43
let resolve = f => path . join ( dir , f )
44
44
let routes = files . filter ( f => path . extname ( f ) === '.js' ) . map ( resolve )
@@ -48,19 +48,22 @@ function findRoutes(dir) {
48
48
49
49
const val = v => ( typeof v === 'undefined' ? 0 : v )
50
50
51
- // allow user to define a custom character for parameterized routes, using ':' as a default
52
- module . exports = function router ( routesDir , paramChar = ':' ) {
53
- paramPattern = new RegExp ( `${ paramChar } ([^\/]+)` ) ;
54
-
51
+ module . exports = function router ( routesDir , config ) {
52
+
55
53
const routes = findRoutes ( routesDir )
54
+ // if filter function is set, filter routes
55
+ . filter ( config && config . filter || function ( ) { return true } )
56
56
// require route files, then add a 'path' property to them
57
57
// the path is in the form of '/path/file', relative to routesDir
58
58
. map ( routeFile => {
59
59
let route = require ( routeFile )
60
60
let extPattern = new RegExp ( path . extname ( routeFile ) + '$' )
61
61
if ( ! route . path ) {
62
- // Sanitize routesDir for cross OS compatibility
63
- route . path = '/' + path . relative ( routesDir , routeFile ) . replace ( extPattern , '' ) . split ( '\\' ) . join ( '/' )
62
+
63
+ route . path = '/' + path . relative ( routesDir , routeFile ) . replace ( extPattern , '' )
64
+ //Fix issue with windows paths
65
+ route . path = route . path . replace ( / \\ / , '/' )
66
+
64
67
}
65
68
return route
66
69
} )
@@ -75,7 +78,7 @@ module.exports = function router(routesDir, paramChar = ':') {
75
78
. sort ( ( a , b ) => val ( a . priority ) < val ( b . priority ) ? 1 : - 1 )
76
79
77
80
// generated match method - call with a req object to get a route.
78
- return function match ( req ) {
81
+ return function match ( req ) {
79
82
let routeFn = r => r [ req . method ] || ( typeof r === 'function' && r )
80
83
let found = routes . find ( r => {
81
84
let matched = r . match ( req . url )
0 commit comments