From 6cbfdbd9755501dbb2d49387d042e684a3b72c16 Mon Sep 17 00:00:00 2001 From: Jacob Samuel G Date: Wed, 13 Jun 2018 20:11:27 -0500 Subject: [PATCH 1/5] Specific route in express A new option was added that allows to use a specific route with the middleware provided by staticify. This option is used by the getVersionedPath method. The specified route for the middleware is added before the versioned path. When a specific route is not used, the method continues to function normally. Ref issue #15 --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 90b0106..7d34a78 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ const staticify = (root, options) => { let defaultOptions = { includeAll: opts.includeAll || false, shortHash: opts.shortHash || true, + mwRoute: opts.mwRoute || '/', sendOptions: opts.sendOptions || {} }; @@ -74,7 +75,7 @@ const staticify = (root, options) => { fileNameParts.push(versions[p], fileNameParts.pop()); - return path.posix.join(path.dirname(p), fileNameParts.join('.')); + return path.posix.join(opts.mwRoute, path.dirname(p), fileNameParts.join('.')); }; // index..js -> index.js From da469d12c5798c25480b81def7336446402ba303 Mon Sep 17 00:00:00 2001 From: Jacob Samuel G Date: Wed, 13 Jun 2018 20:23:20 -0500 Subject: [PATCH 2/5] README updated The description and example of the new specific route option was added in the README file. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 5ffa8ec..1bea7fb 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,24 @@ Generate a short (7-digit) md5 hash instead of the full (32-digit) one. * Type: Boolean * Default: `true` +### mwRoute + +If you are using the staticify convenience middleware through a specific route, it is necessary to indicate the route in this option. + +* Type: String +* Default: "/" + +```js +// Example + +// ... +var options = { mwRoute: '/assets' } +var staticify = require('staticify')(path.join(__dirname, 'public'), options); + +// ... +app.use('/assets', staticify.middleware); // `app` is your express instance +``` + ### sendOptions * Type: Object From 2f04e2ed043e1693b6022b7196a471318fd0b8fa Mon Sep 17 00:00:00 2001 From: Jacob Samuel G Date: Thu, 14 Jun 2018 16:05:21 -0500 Subject: [PATCH 3/5] Add a test case for `mwRoute` option. --- test/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/index.js b/test/index.js index 73ac87f..99918b1 100644 --- a/test/index.js +++ b/test/index.js @@ -60,6 +60,17 @@ describe('.getVersionedPath', () => { /^[0-9a-f]{32}$/i.exec(versioned[1])[0].should.equal(versioned[1]); }); + it('should add a prefix route to the path', () => { + let versioned = staticify(ROOT, {mwRoute: '/prefix'}).getVersionedPath('/index.js'); + + versioned = versioned.split('.'); + versioned.should.have.a.lengthOf(3); + versioned[0].should.equal('/prefix/index'); + versioned[2].should.equal('js'); + versioned[1].should.have.a.lengthOf(7); + /^[0-9a-f]{7}$/i.exec(versioned[1])[0].should.equal(versioned[1]); + }); + it('shouldn\'t add a hash if the path isn\'t known', () => { staticify(ROOT).getVersionedPath('/unknown.js').should.equal('/unknown.js'); }); From c694f673061ba0e09d931272c885f930bd0e2b72 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 15 Jun 2018 11:07:01 +0300 Subject: [PATCH 4/5] Update README.md --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1bea7fb..514db07 100644 --- a/README.md +++ b/README.md @@ -67,13 +67,10 @@ If you are using the staticify convenience middleware through a specific route, * Default: "/" ```js -// Example - -// ... -var options = { mwRoute: '/assets' } +var path = require('path'); +var options = { mwRoute: '/assets' }; var staticify = require('staticify')(path.join(__dirname, 'public'), options); -// ... app.use('/assets', staticify.middleware); // `app` is your express instance ``` From 3ceeced2f4f063bb4967e3d9c5201ea8612dae81 Mon Sep 17 00:00:00 2001 From: Jacob Samuel G Date: Fri, 15 Jun 2018 11:25:02 -0500 Subject: [PATCH 5/5] Change mwRoute to pathPrefix name. --- README.md | 4 ++-- index.js | 4 ++-- test/index.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 514db07..2bde91d 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Generate a short (7-digit) md5 hash instead of the full (32-digit) one. * Type: Boolean * Default: `true` -### mwRoute +### pathPrefix If you are using the staticify convenience middleware through a specific route, it is necessary to indicate the route in this option. @@ -68,7 +68,7 @@ If you are using the staticify convenience middleware through a specific route, ```js var path = require('path'); -var options = { mwRoute: '/assets' }; +var options = { pathPrefix: '/assets' }; var staticify = require('staticify')(path.join(__dirname, 'public'), options); app.use('/assets', staticify.middleware); // `app` is your express instance diff --git a/index.js b/index.js index 7d34a78..afbdb9d 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ const staticify = (root, options) => { let defaultOptions = { includeAll: opts.includeAll || false, shortHash: opts.shortHash || true, - mwRoute: opts.mwRoute || '/', + pathPrefix: opts.pathPrefix || '/', sendOptions: opts.sendOptions || {} }; @@ -75,7 +75,7 @@ const staticify = (root, options) => { fileNameParts.push(versions[p], fileNameParts.pop()); - return path.posix.join(opts.mwRoute, path.dirname(p), fileNameParts.join('.')); + return path.posix.join(opts.pathPrefix, path.dirname(p), fileNameParts.join('.')); }; // index..js -> index.js diff --git a/test/index.js b/test/index.js index 99918b1..6e5cbaf 100644 --- a/test/index.js +++ b/test/index.js @@ -61,7 +61,7 @@ describe('.getVersionedPath', () => { }); it('should add a prefix route to the path', () => { - let versioned = staticify(ROOT, {mwRoute: '/prefix'}).getVersionedPath('/index.js'); + let versioned = staticify(ROOT, {pathPrefix: '/prefix'}).getVersionedPath('/index.js'); versioned = versioned.split('.'); versioned.should.have.a.lengthOf(3);