diff --git a/README.md b/README.md index 657b582..2ccf7ae 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.js b/src/index.js index 605b6c7..2483e6b 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/test/helpers/mock-middleware.js b/test/helpers/mock-middleware.js new file mode 100644 index 0000000..d278e78 --- /dev/null +++ b/test/helpers/mock-middleware.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = function(req, res) { + res.set('x-test-middleware', 'true'); + res.send(req.html); +}; \ No newline at end of file diff --git a/test/helpers/test-http-server.js b/test/helpers/test-http-server.js index 7bf8dca..9c22551 100644 --- a/test/helpers/test-http-server.js +++ b/test/helpers/test-http-server.js @@ -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; @@ -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) => { diff --git a/test/middleware-test.js b/test/middleware-test.js index cc9b0d0..d592f35 100644 --- a/test/middleware-test.js +++ b/test/middleware-test.js @@ -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/); + }); + }); + }); });