Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 03fdebb

Browse files
committed
added middelware to provide path for the bundle(s)
1 parent e47c80a commit 03fdebb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

examples/simple.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
'use strict';
22

33
const express = require('express');
4+
const Assets = require('../');
45
const app = express();
56

6-
const Assets = require('../');
7+
// set up asset bundler
78
const assets = new Assets('./assets/es5/main.js');
89

10+
// listen for events
911
assets.on('update', (ids) => {
1012
ids.forEach((id) => {
1113
console.log('updated:', id);
1214
});
1315
});
1416

17+
// provide bundle info to each request
18+
app.use(assets.middelware());
19+
20+
// attach bundle routes
1521
app.use(assets.router());
1622

23+
// print where bundle is
24+
app.get('/', (req, res, next) => {
25+
res.status(200).send(`JS bunde is at: ${res.locals.js}`);
26+
})
27+
28+
// start server
1729
const server = app.listen(8080, () => {
1830
console.log(`http server - Bundle at: http://localhost:${server.address().port}/js/`);
1931
});

lib/middleware.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = class Middleware extends EventEmitter {
1616
cache: {},
1717
packageCache: {},
1818
debug: true,
19-
jsBundlePath: '/js',
19+
jsPath: '/js',
2020
}, options);
2121

2222
this.emits = emits;
@@ -44,7 +44,8 @@ module.exports = class Middleware extends EventEmitter {
4444
this.app = express.Router({ // eslint-disable-line
4545
mergeParams: true,
4646
});
47-
this.app.get(this.options.jsBundlePath, this.js());
47+
48+
this.app.get(this.options.jsPath, this.js());
4849
}
4950

5051

@@ -58,6 +59,14 @@ module.exports = class Middleware extends EventEmitter {
5859
}
5960

6061

62+
middelware (jsProp = 'js') {
63+
return (req, res, next) => {
64+
res.locals[jsProp] = this.options.jsPath;
65+
next();
66+
}
67+
}
68+
69+
6170
router () {
6271
return this.app;
6372
}

0 commit comments

Comments
 (0)