Skip to content

Commit 3cd0e6a

Browse files
jpbourgeonjesseditson
authored andcommitted
Add support for % as a param delimiter (#2)
Windows does not support paths starting with `:` - this adds support for either `%` or `:` to indicate a parameter.
1 parent a995cb4 commit 3cd0e6a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ const path = require('path')
22
const fs = require('fs')
33
const qs = require('querystring')
44

5-
const paramPattern = /:([^/]+)/
5+
6+
const paramPattern = /(?::|%)([^\/]+)/
7+
68
// takes routes and decorates them with a 'match' method that will return { params, query } if a path matches
79
function addMatch (route) {
810
let routePath = route.path
911
let paramNames = []
1012
let matched
11-
// find any paths prefixed with a `:`, and treat them as capture groups
13+
// find any paths prefixed with `:` or `%`, and treat them as capture groups
1214
while ((matched = routePath.match(paramPattern)) !== null) {
1315
routePath = routePath.replace(paramPattern, '([^?/]+)')
1416
paramNames.push(matched[1])
1517
}
1618
// if a route ends with `index`, allow matching that route without matching the `index` part
1719
if (path.basename(routePath) === 'index') {
1820
route.isIndex = true
19-
routePath = routePath.replace(/\/index$/, '/?(:?index)?')
21+
routePath = routePath.replace(/\/index$/, '/?([:%]?index)?')
2022
}
2123
// create a regex with our path
2224
let pattern = new RegExp(`^${routePath}(\\?(.*)|$)`, 'i')
@@ -45,7 +47,9 @@ function findRoutes (dir) {
4547
}
4648

4749
const val = v => (typeof v === 'undefined' ? 0 : v)
50+
4851
module.exports = function router (routesDir, config) {
52+
4953
const routes = findRoutes(routesDir)
5054
// if filter function is set, filter routes
5155
.filter(config && config.filter || function () { return true })
@@ -55,9 +59,11 @@ module.exports = function router (routesDir, config) {
5559
let route = require(routeFile)
5660
let extPattern = new RegExp(path.extname(routeFile) + '$')
5761
if (!route.path) {
62+
5863
route.path = '/' + path.relative(routesDir, routeFile).replace(extPattern, '')
5964
//Fix issue with windows paths
6065
route.path = route.path.replace(/\\/, '/')
66+
6167
}
6268
return route
6369
})

0 commit comments

Comments
 (0)