Skip to content

Commit 38e2e01

Browse files
jacsamgXhmikosR
authored andcommitted
Add support to specific route in express (#18)
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.
1 parent 93b6c4b commit 38e2e01

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ Generate a short (7-digit) md5 hash instead of the full (32-digit) one.
5959
* Type: Boolean
6060
* Default: `true`
6161

62+
### pathPrefix
63+
64+
If you are using the staticify convenience middleware through a specific route, it is necessary to indicate the route in this option.
65+
66+
* Type: String
67+
* Default: "/"
68+
69+
```js
70+
var path = require('path');
71+
var options = { pathPrefix: '/assets' };
72+
var staticify = require('staticify')(path.join(__dirname, 'public'), options);
73+
74+
app.use('/assets', staticify.middleware); // `app` is your express instance
75+
```
76+
6277
### sendOptions
6378

6479
* Type: Object

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const staticify = (root, options) => {
1717
let defaultOptions = {
1818
includeAll: opts.includeAll || false,
1919
shortHash: opts.shortHash || true,
20+
pathPrefix: opts.pathPrefix || '/',
2021
sendOptions: opts.sendOptions || {}
2122
};
2223

@@ -74,7 +75,7 @@ const staticify = (root, options) => {
7475

7576
fileNameParts.push(versions[p], fileNameParts.pop());
7677

77-
return path.posix.join(path.dirname(p), fileNameParts.join('.'));
78+
return path.posix.join(opts.pathPrefix, path.dirname(p), fileNameParts.join('.'));
7879
};
7980

8081
// index.<hash>.js -> index.js

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ describe('.getVersionedPath', () => {
6060
/^[0-9a-f]{32}$/i.exec(versioned[1])[0].should.equal(versioned[1]);
6161
});
6262

63+
it('should add a prefix route to the path', () => {
64+
let versioned = staticify(ROOT, {pathPrefix: '/prefix'}).getVersionedPath('/index.js');
65+
66+
versioned = versioned.split('.');
67+
versioned.should.have.a.lengthOf(3);
68+
versioned[0].should.equal('/prefix/index');
69+
versioned[2].should.equal('js');
70+
versioned[1].should.have.a.lengthOf(7);
71+
/^[0-9a-f]{7}$/i.exec(versioned[1])[0].should.equal(versioned[1]);
72+
});
73+
6374
it('shouldn\'t add a hash if the path isn\'t known', () => {
6475
staticify(ROOT).getVersionedPath('/unknown.js').should.equal('/unknown.js');
6576
});

0 commit comments

Comments
 (0)