Skip to content

Commit fb2913f

Browse files
authored
Changes for Windows compatibility
Windows uses backslashes for OS paths and doesn't allow semicolons in folders and files names. I introduced 2 changes in the router to handle these behaviours : - an optional paramChar to let the user customized the character used for parameterized routes. Semicolon is the default value. I moved the paramPattern definition inside the router accordingly, to apply the user's choice - a sanitization of the paths to prevent mixing of slashes and backslashes which messes the parameters's value indentification
1 parent 4492000 commit fb2913f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

index.js

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

5-
const paramPattern = /:([^\/]+)/
5+
// create global paramPattern but define it in the router to allow custom char for parameterized routes
6+
let 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
@@ -45,15 +47,20 @@ function findRoutes(dir) {
4547
}
4648

4749
const val = v => (typeof v === 'undefined' ? 0 : v)
48-
module.exports = function router(routesDir) {
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+
4955
const routes = findRoutes(routesDir)
5056
// require route files, then add a 'path' property to them
5157
// the path is in the form of '/path/file', relative to routesDir
5258
.map(routeFile => {
5359
let route = require(routeFile)
5460
let extPattern = new RegExp(path.extname(routeFile) + '$')
5561
if (!route.path) {
56-
route.path = '/' + path.relative(routesDir, routeFile).replace(extPattern, '')
62+
// Sanitize routesDir for cross OS compatibility
63+
route.path = '/' + path.relative(routesDir, routeFile).replace(extPattern, '').split('\\').join('/')
5764
}
5865
return route
5966
})

0 commit comments

Comments
 (0)