|
| 1 | +# FastBoot Express Middleware |
| 2 | + |
| 3 | +[](https://greenkeeper.io/) |
| 4 | + |
| 5 | +[](https://travis-ci.org/ember-fastboot/fastboot-express-middleware) |
| 6 | + |
| 7 | +This middleware is a small wrapper around the |
| 8 | +[fastboot](https://github.com/ember-fastboot/fastboot) package, which |
| 9 | +renders Ember.js apps in Node.js. |
| 10 | + |
| 11 | +By adding this middleware to your Express app, you can serve HTML from a |
| 12 | +rendered Ember.js app to clients that don't support JavaScript, such as |
| 13 | +`curl`, search crawlers, or users with JavaScript disabled. |
| 14 | + |
| 15 | +Note that this is _just an Express middleware_ and there is more needed |
| 16 | +to serve apps in a production environment. If you want to server-side |
| 17 | +rendered Ember applications without doing a lot of work, you are |
| 18 | +recommended to consider the [FastBoot App |
| 19 | +Server](https://github.com/ember-fastboot/fastboot-app-server), which |
| 20 | +manages many of the hard parts for you. |
| 21 | + |
| 22 | +That said, this middleware is designed to be easy to integrate for those |
| 23 | +who already have existing Express stacks, or who want maximum |
| 24 | +flexibility in how requests are handled. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```js |
| 29 | +const express = require('express'); |
| 30 | +const fastbootMiddleware = require('fastboot-express-middleware'); |
| 31 | + |
| 32 | +let app = express(); |
| 33 | + |
| 34 | +app.get('/*', fastbootMiddleware('/path/to/dist')); |
| 35 | + |
| 36 | +app.listen(3000, function () { |
| 37 | + console.log('FastBoot app listening on port 3000!'); |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Building Your Ember App |
| 42 | + |
| 43 | +Before you can use your app with FastBoot, you must first install the |
| 44 | +[ember-cli-fastboot][ember-cli-fastboot] addon and build your app by |
| 45 | +running `ember build`. The build process will compile your app into a |
| 46 | +version that is compatible with both Node.js and the browser and put it |
| 47 | +in the `dist` directory. This `dist` directory is the path you should |
| 48 | +provide to the middleware to specify which Ember app to load and render. |
| 49 | + |
| 50 | +## Resilient Mode |
| 51 | + |
| 52 | +By default, errors during render will cause the middleware to send an |
| 53 | +HTTP 500 status code as the response. In order to swallow errors and |
| 54 | +return a `200` status code with an empty HTML page, set the `resilient` flag to |
| 55 | +true: |
| 56 | + |
| 57 | +```js |
| 58 | +app.get('/*', fastbootMiddleware({ |
| 59 | + distPath: '/path/to/dist', |
| 60 | + resilient: true |
| 61 | +})); |
| 62 | +``` |
| 63 | + |
| 64 | +Resilient mode still calls `next(err)` to propagate your error to any subsequent |
| 65 | +middleware that you apply after this one. |
| 66 | +You can use this feature to track errors or log analytics. |
| 67 | + |
| 68 | +However, because FastBoot is reslient still sends the response to the client. |
| 69 | +***You cannot alter the `response`*** with any of your post-fastboot middleware. |
| 70 | + |
| 71 | +## Custom FastBoot Instance |
| 72 | + |
| 73 | +For more control over the FastBoot instance that is created to render |
| 74 | +the Ember app, you can pass a custom instance that the middleware will |
| 75 | +use instead of creating its own: |
| 76 | + |
| 77 | +```js |
| 78 | +let fastboot = new FastBoot({ |
| 79 | + distPath: 'path/to/dist' |
| 80 | +}); |
| 81 | + |
| 82 | +let middleware = fastbootMiddleware({ |
| 83 | + fastboot: fastboot |
| 84 | +}); |
| 85 | + |
| 86 | +app.get('/*', middleware); |
| 87 | + |
| 88 | +// ...later |
| 89 | +fastboot.reload(); |
| 90 | +``` |
| 91 | + |
| 92 | +## Response chunking |
| 93 | + |
| 94 | +By default, the middleware writes the complete response at once but response |
| 95 | +chunking (aka HTTP Streaming) is available via a config switch: |
| 96 | + |
| 97 | +```js |
| 98 | +app.get('/*', fastbootMiddleware({ |
| 99 | + distPath: '/path/to/dist', |
| 100 | + chunkedResponse: true |
| 101 | +})); |
| 102 | +``` |
| 103 | + |
| 104 | +Enabling response chunking will result in the response being delivered in |
| 105 | +multiple chunks (one for the head, one for the body and one for each shoebox) |
| 106 | +which helps getting the HTML to clients faster. |
| 107 | + |
| 108 | +[ember-cli-fastboot]: https://github.com/ember-fastboot/ember-cli-fastboot |
| 109 | + |
| 110 | +## VisitOptions |
| 111 | + |
| 112 | +For sending over additional metadata so that it could be leveraged by the consuming app/addon, you can pass the `visitOptions` option that contains any extra information that might be necessary. |
| 113 | + |
| 114 | +Example usecase: If an addon relies on some metadata that is set by the consuming app, then in that case the addon will not have the access to the metadata value. In such cases, developing against dummy app becomes difficult. Hence, passing in the `visitOptions` will enable smoother local addon development. |
| 115 | + |
| 116 | +```js |
| 117 | +app.get('/*', fastbootMiddleware({ |
| 118 | + distPath: '/path/to/dist', |
| 119 | + visitOptions: { |
| 120 | + metadata: { |
| 121 | + foo: 'bar' |
| 122 | + } |
| 123 | + } |
| 124 | +})); |
| 125 | +``` |
| 126 | + |
| 127 | +## Tests |
| 128 | + |
| 129 | +`npm test` |
0 commit comments