|
| 1 | +# FastBoot Express Middleware |
| 2 | + |
| 3 | +This middleware is a small wrapper around the |
| 4 | +[`fastboot`](https://github.com/ember-fastboot/fastboot) package, which |
| 5 | +renders Ember.js apps in Node.js. |
| 6 | + |
| 7 | +By adding this middleware to your Express app, you can serve HTML from a |
| 8 | +rendered Ember.js app to clients that don't support JavaScript, such as |
| 9 | +`curl`, search crawlers, or users with JavaScript disabled. |
| 10 | + |
| 11 | +Note that this is _just an Express middleware_ and there is more needed |
| 12 | +to serve apps in a production environment. If you want to server-side |
| 13 | +rendered Ember applications without doing a lot of work, you are |
| 14 | +recommended to consider the [FastBoot App |
| 15 | +Server](https://github.com/ember-fastboot/fastboot-app-server), which |
| 16 | +manages many of the hard parts for you. |
| 17 | + |
| 18 | +That said, this middleware is designed to be easy to integrate for those |
| 19 | +who already have existing Express stacks, or who want maximum |
| 20 | +flexibility in how requests are handled. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +```js |
| 25 | +const express = require('express'); |
| 26 | +const fastbootMiddleware = require('fastboot-express-middleware'); |
| 27 | + |
| 28 | +let app = express(); |
| 29 | + |
| 30 | +app.get('/*', fastbootMiddleware('/path/to/dist')); |
| 31 | + |
| 32 | +app.listen(3000, function () { |
| 33 | + console.log('FastBoot app listening on port 3000!'); |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +## Building Your Ember App |
| 38 | + |
| 39 | +Before you can use your app with FastBoot, you must first install the |
| 40 | +[ember-cli-fastboot][ember-cli-fastboot] addon and build your app by |
| 41 | +running `ember build`. The build process will compile your app into a |
| 42 | +version that is compatible with both Node.js and the browser and put it |
| 43 | +in the `dist` directory. This `dist` directory is the path you should |
| 44 | +provide to the middleware to specify which Ember app to load and render. |
| 45 | + |
| 46 | +## Resilient Mode |
| 47 | + |
| 48 | +By default, errors during render will cause the middleware to send an |
| 49 | +HTTP 500 status code as the response. In order to swallow errors and |
| 50 | +return a `200 OK` with an empty HTML page, set the `resilient` flag to |
| 51 | +true: |
| 52 | + |
| 53 | +```js |
| 54 | +app.get('/*', fastbootMiddleware('/path/to/dist', { |
| 55 | + resilient: true |
| 56 | +})); |
| 57 | +``` |
| 58 | + |
| 59 | +## Custom FastBoot Instance |
| 60 | + |
| 61 | +For more control over the FastBoot instance that is created to render |
| 62 | +the Ember app, you can pass a custom instance that the middleware will |
| 63 | +use instead of creating its own: |
| 64 | + |
| 65 | +```js |
| 66 | +let fastboot = new FastBoot({ |
| 67 | + distPath: 'path/to/dist' |
| 68 | +}); |
| 69 | + |
| 70 | +let middleware = fastbootMiddleware({ |
| 71 | + fastboot: fastboot |
| 72 | +}); |
| 73 | + |
| 74 | +app.get('/*', middleware); |
| 75 | + |
| 76 | +// ...later |
| 77 | +fastboot.reload(); |
| 78 | +``` |
| 79 | + |
| 80 | +[ember-cli-fastboot]: https://github.com/ember-fastboot/ember-cli-fastboot |
0 commit comments