Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to enable post processing fastboot response #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ app.get('/*', middleware);
fastboot.reload();
```

## Post processing

If you want to work on the html response after fastboot is done with its work,
you can set an option to include other middleware's in the chain.

```js
var myMiddleware = function(req, res) {
res.send(req.html);
};

app.use(fastbootMiddleware({
distPath: 'path/to/dist',
postProcess: true
}));
app.get('/*', myMiddleware);
```

[ember-cli-fastboot]: https://github.com/ember-fastboot/ember-cli-fastboot

## Tests
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function fastbootExpressMiddleware(distPath, options) {
next(result.error);
}

if (opts.postProcess) {
req.html = html;
return next();
}

log(result.statusCode, statusMessage + path);
res.status(result.statusCode);
res.send(html);
Expand Down
6 changes: 6 additions & 0 deletions test/helpers/mock-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(req, res) {
res.set('x-test-middleware', 'true');
res.send(req.html);
};
9 changes: 8 additions & 1 deletion test/helpers/test-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const express = require('express');
const request = require('request-promise');
const fastbootMiddleware = require('./../../src/index');
const mockMiddleware = require('./mock-middleware');

let serverID = 0;

Expand All @@ -18,7 +19,13 @@ class TestHTTPServer {
let options = this.options;
let app = express();

app.get('/*', this.middleware);
if (options.postProcess) {
app.use(this.middleware);
app.use(mockMiddleware);
app.get('/*');
} else {
app.get('/*', this.middleware);
}

if (options.errorHandling) {
app.use((err, req, res, next) => {
Expand Down
16 changes: 16 additions & 0 deletions test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,20 @@ describe("FastBoot", function() {
});
});

describe('checks middleware chain', function() {
it('chains when it has post process', function() {
let middleware = fastbootMiddleware({
distPath: fixture('basic-app'),
postProcess: true
});
server = new TestHTTPServer(middleware, { postProcess: true });

return server.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, statusCode, headers }) => {
expect(statusCode).to.equal(200);
expect(headers['x-test-middleware']).to.match(/true/);
});
});
});
});