Skip to content

Commit ea0be49

Browse files
author
Arjan Singh
committed
pre-FastBoot and post-FastBoot middleware hooks
1 parent 0af4720 commit ea0be49

File tree

12 files changed

+84195
-7
lines changed

12 files changed

+84195
-7
lines changed

src/express-http-server.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const express = require('express');
44
const basicAuth = require('./basic-auth');
55

6+
function noop() {}
7+
68
class ExpressHTTPServer {
79
constructor(options) {
810
options = options || {};
@@ -13,8 +15,8 @@ class ExpressHTTPServer {
1315
this.password = options.password;
1416
this.cache = options.cache;
1517
this.gzip = options.gzip || false;
16-
this.preFastbootMiddlewares = options.preFastbootMiddlewares || [];
17-
this.postFastbootMiddlewares = options.postFastbootMiddlewares || [];
18+
this.preFastbootMiddlewares = options.preFastbootMiddlewares || noop;
19+
this.postFastbootMiddlewares = options.postFastbootMiddlewares || noop;
1820

1921
this.app = express();
2022
}
@@ -24,7 +26,7 @@ class ExpressHTTPServer {
2426
let username = this.username;
2527
let password = this.password;
2628

27-
this.preFastbootMiddlewares.forEach(args => app.use(...args));
29+
this.preFastbootMiddlewares(app);
2830

2931
if (this.gzip) {
3032
this.app.use(require('compression')());
@@ -49,7 +51,7 @@ class ExpressHTTPServer {
4951

5052
app.get('/*', fastbootMiddleware);
5153

52-
this.postFastbootMiddlewares.forEach(args => app.use(...args));
54+
this.postFastbootMiddlewares(app);
5355

5456
return new Promise(resolve => {
5557
let listener = app.listen(process.env.PORT || 3000, () => {

src/fastboot-app-server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class FastBootAppServer {
3333
distPath: this.distPath || process.env.FASTBOOT_DIST_PATH,
3434
cache: this.cache,
3535
gzip: this.gzip,
36-
httpServer: this.httpServer
36+
httpServer: this.httpServer,
37+
preFastbootMiddlewares: this.preFastbootMiddlewares,
38+
postFastbootMiddlewares: this.postFastbootMiddlewares
3739
});
3840

3941
this.worker.start();

src/worker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class Worker {
1919
ui: this.ui,
2020
distPath: this.distPath,
2121
cache: this.cache,
22-
gzip: this.gzip
22+
gzip: this.gzip,
23+
preFastbootMiddlewares: this.preFastbootMiddlewares,
24+
postFastbootMiddlewares: this.postFastbootMiddlewares,
2325
});
2426
}
2527

@@ -60,7 +62,6 @@ class Worker {
6062
buildMiddleware() {
6163
this.fastboot = new FastBoot({
6264
distPath: this.distPath,
63-
resilient: true
6465
});
6566

6667
return fastbootMiddleware({

test/app-server-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ describe("FastBootAppServer", function() {
7070
});
7171
});
7272

73+
it("executes preFastbootMiddlewares", function() {
74+
return runServer('prefastboot-middleware-server')
75+
.then(() => request('http://localhost:3000'))
76+
.then(response => {
77+
expect(response.statusCode).to.equal(418);
78+
expect(response.headers['x-test-header']).to.equal('testing');
79+
expect(response.body).to.equal(JSON.stringify({ send: 'json back'}));
80+
});
81+
});
82+
83+
it("executes postFastbootMiddlewares when there is an error", function() {
84+
return runServer('postfastboot-middleware-server')
85+
.then(() => request('http://localhost:3000'))
86+
.then(response => {
87+
expect(response.body).to.not.match(/error/);
88+
expect(response.headers['x-test-header']).to.equal('testing');
89+
})
90+
});
91+
7392
});
7493

7594
function runServer(name) {

0 commit comments

Comments
 (0)