diff --git a/src/index.js b/src/index.js index 29edd06..8aa1cea 100644 --- a/src/index.js +++ b/src/index.js @@ -5,17 +5,24 @@ const mountRoutes = require('./mountRoutes'); module.exports = function( root, - { cwd = process.cwd(), viewExtensions = [] } = {} + { cwd = process.cwd(), viewExtensions = [], paramChar = '$' } = {} ) { root = path.resolve(cwd, root); // The base router that'll hold everything const router = Router(); // First mount the middlewares - const routesFromMiddlewares = mountMiddlewares(router, root, { cwd }); + const routesFromMiddlewares = mountMiddlewares(router, root, { + cwd, + paramChar + }); // And then the actual routes - mountRoutes(router, root, { routes: routesFromMiddlewares, viewExtensions }); + mountRoutes(router, root, { + routes: routesFromMiddlewares, + viewExtensions, + paramChar + }); return router; }; diff --git a/src/mountMiddlewares.js b/src/mountMiddlewares.js index bca2f2f..5f6e53f 100644 --- a/src/mountMiddlewares.js +++ b/src/mountMiddlewares.js @@ -6,7 +6,7 @@ const toExpressPath = require('./toExpressPath'); const compareRouteDepth = require('./compareRouteDepth'); const compareRouteVariability = require('./compareRouteVariability'); -module.exports = function mountMiddlewares(router, root) { +module.exports = function mountMiddlewares(router, root, { paramChar }) { // Extra routes to configure const routesFromMiddlewares = []; // First let's list the routes file @@ -19,7 +19,7 @@ module.exports = function mountMiddlewares(router, root) { return { // We need the express routePath here so that // variables are properly detected when sorting - routePath: toExpressPath(path.dirname(filePath)), + routePath: toExpressPath(path.dirname(filePath), { paramChar }), config }; }) diff --git a/src/mountRoutes.js b/src/mountRoutes.js index 1b1a1e6..6f1fa88 100644 --- a/src/mountRoutes.js +++ b/src/mountRoutes.js @@ -8,10 +8,10 @@ const compareRouteVariability = require('./compareRouteVariability'); module.exports = function mountRoutes( router, root, - { routes = [], viewExtensions = [] } + { routes = [], viewExtensions = [], paramChar } ) { routes - .concat(discoverRoutes(root, { viewExtensions })) + .concat(discoverRoutes(root, { viewExtensions, paramChar })) .sort((routeA, routeB) => { return ( -1 * compareRouteDepth(routeA.routePath, routeB.routePath) || @@ -25,7 +25,7 @@ module.exports = function mountRoutes( }); }; -function discoverRoutes(root, { viewExtensions }) { +function discoverRoutes(root, { viewExtensions, paramChar }) { return fastGlob .sync( `**/{*.,}(${HTTP_METHODS.join('|')}).(${['js', ...viewExtensions].join( @@ -42,7 +42,7 @@ function discoverRoutes(root, { viewExtensions }) { const handler = getHandler(fullPath, extension); return { fullPath, - routePath: toExpressPath(routePath), + routePath: toExpressPath(routePath, { paramChar }), method, extension, handler diff --git a/src/toExpressPath.js b/src/toExpressPath.js index 1a1ca54..7cc0f0c 100644 --- a/src/toExpressPath.js +++ b/src/toExpressPath.js @@ -1,6 +1,8 @@ -module.exports = function toExpressPath(routePath) { +module.exports = function toExpressPath(routePath, { paramChar }) { if (routePath) { - return routePath.replace(/\$/g, ':').replace(/__/g, '/'); + return routePath + .replace(new RegExp(`\\${paramChar}`, 'g'), ':') + .replace(/__/g, '/'); } else { return '/'; }