Skip to content

Commit d5e5a80

Browse files
author
Arjan Singh
committed
beforeMiddleware and and afterMiddleware hooks
1 parent 0af4720 commit d5e5a80

File tree

13 files changed

+84215
-11
lines changed

13 files changed

+84215
-11
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ let server = new FastBootAppServer({
103103
server.start();
104104
```
105105

106+
## Pre and Post FastBoot middleware hooks
107+
108+
If you need something less than a custom server and just want to run some middleware
109+
before or after FastBoot runs, the server provides hooks for you to do so:
110+
111+
```js
112+
// Custom Middlewares
113+
function modifyRequest(req, res, next) { /* do pre-fastboot stuff to `req` */ };
114+
function handleErrors(err, req, res, next) { /* do error recovery stuff */ };
115+
116+
const server = FastBootAppServer({
117+
beforeMiddleware: function (app) { app.use(modifyRequest); },
118+
afterMiddleware: function (app) { app.use(handleErrors); }
119+
})
120+
```
121+
106122
## Downloaders
107123

108124
You can point the app server at a static path that you manage, but that

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.beforeMiddleware = options.beforeMiddleware || noop;
19+
this.afterMiddleware = options.afterMiddleware || 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.beforeMiddleware(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.afterMiddleware(app);
5355

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

src/fastboot-app-server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class FastBootAppServer {
1717
this.ui = options.ui;
1818
this.gzip = options.gzip;
1919
this.httpServer = options.httpServer;
20-
this.preFastbootMiddlewares = options.preFastbootMiddlewares;
21-
this.postFastbootMiddlewares = options.postFastbootMiddlewares;
20+
this.beforeMiddleware = options.beforeMiddleware;
21+
this.afterMiddleware = options.afterMiddleware;
2222

2323
if (!this.ui) {
2424
let UI = require('./ui');
@@ -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+
beforeMiddleware: this.beforeMiddleware,
38+
afterMiddleware: this.afterMiddleware
3739
});
3840

3941
this.worker.start();

src/worker.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ class Worker {
1111
this.ui = options.ui;
1212
this.cache = options.cache;
1313
this.gzip = options.gzip;
14-
this.preFastbootMiddlewares = options.preFastbootMiddlewares;
15-
this.postFastbootMiddlewares = options.postFastbootMiddlewares;
14+
this.beforeMiddleware = options.beforeMiddleware;
15+
this.afterMiddleware = options.afterMiddleware;
1616

1717
if (!this.httpServer) {
1818
this.httpServer = new ExpressHTTPServer({
1919
ui: this.ui,
2020
distPath: this.distPath,
2121
cache: this.cache,
22-
gzip: this.gzip
22+
gzip: this.gzip,
23+
beforeMiddleware: this.beforeMiddleware,
24+
afterMiddleware: this.afterMiddleware,
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 beforeMiddleware", function() {
74+
return runServer('before-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 afterMiddleware when there is an error", function() {
84+
return runServer('after-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) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var alchemistRequire = require('broccoli-module-alchemist/require');
5+
var FastBootAppServer = alchemistRequire('fastboot-app-server');
6+
7+
function setXTestHeader(err, req, res, next) {
8+
res.set('x-test-header', 'testing')
9+
next();
10+
}
11+
12+
var server = new FastBootAppServer({
13+
distPath: path.resolve(__dirname, './broken-app'),
14+
afterMiddleware: function (app) {
15+
app.use(setXTestHeader);
16+
},
17+
resilient: true,
18+
});
19+
20+
server.start();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var alchemistRequire = require('broccoli-module-alchemist/require');
5+
var FastBootAppServer = alchemistRequire('fastboot-app-server');
6+
7+
function setStatusCode418(req, res, next) {
8+
res.status(418);
9+
next();
10+
}
11+
12+
function setXTestHeader(req, res, next) {
13+
res.set('X-Test-Header', 'testing')
14+
next();
15+
}
16+
17+
function sendJsonAndTerminate(req, res, next) {
18+
res.json({ send: 'json back' });
19+
res.send();
20+
}
21+
22+
var server = new FastBootAppServer({
23+
distPath: path.resolve(__dirname, './basic-app'),
24+
beforeMiddleware: function (app) {
25+
app.use(setStatusCode418);
26+
app.use(setXTestHeader);
27+
app.use(sendJsonAndTerminate);
28+
}
29+
});
30+
31+
server.start();

0 commit comments

Comments
 (0)