|
| 1 | +## Ember FastBoot App Server |
| 2 | + |
| 3 | +The FastBoot App Server is an application server for hosting Ember |
| 4 | +FastBoot apps. It manages downloading the Ember app, starting multiple |
| 5 | +HTTP server processes, and detecting when new versions of the |
| 6 | +application have been deployed. |
| 7 | + |
| 8 | +FastBoot allows Ember apps to be rendered on the server, to support |
| 9 | +things like search crawlers and clients without JavaScript. For more |
| 10 | +information about FastBoot, see |
| 11 | +[FastBoot website][fastboot]. |
| 12 | + |
| 13 | +[fastboot]: https://www.ember-fastboot.com |
| 14 | + |
| 15 | +## Extensibility |
| 16 | + |
| 17 | +The App Server is designed to be flexible and extensible enough to run |
| 18 | +in whatever environment you want to use to host FastBoot apps. In |
| 19 | +particular, you can provide a custom: |
| 20 | + |
| 21 | +* *Downloader*, to control how app builds gets downloaded |
| 22 | +* *Notifier*, to control how new versions of the build are detected |
| 23 | +* *HTTP Server*, to use whatever stack you prefer for serving HTTP |
| 24 | + requests in Node.js |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +FastBoot App Server requires Node.js v10 or later. |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +Put the following in a `fastboot-server.js` file: |
| 33 | + |
| 34 | +```js |
| 35 | +const FastBootAppServer = require('fastboot-app-server'); |
| 36 | + |
| 37 | +const MY_GLOBAL = 'MY GLOBAL'; |
| 38 | + |
| 39 | +let server = new FastBootAppServer({ |
| 40 | + distPath: 'dist', |
| 41 | + gzip: true, // Optional - Enables gzip compression. |
| 42 | + host: '0.0.0.0', // Optional - Sets the host the server listens on. |
| 43 | + port: 4000, // Optional - Sets the port the server listens on (defaults to the PORT env var or 3000). |
| 44 | + buildSandboxGlobals(defaultGlobals) { // Optional - Make values available to the Ember app running in the FastBoot server, e.g. "MY_GLOBAL" will be available as "GLOBAL_VALUE" |
| 45 | + return Object.assign({}, defaultGlobals, { GLOBAL_VALUE: MY_GLOBAL }); |
| 46 | + }, |
| 47 | + chunkedResponse: true // Optional - Opt-in to chunked transfer encoding, transferring the head, body and potential shoeboxes in separate chunks. Chunked transfer encoding should have a positive effect in particular when the app transfers a lot of data in the shoebox. |
| 48 | +}); |
| 49 | + |
| 50 | +server.start(); |
| 51 | +``` |
| 52 | + |
| 53 | +Configure `distPath` to point to the `dist` directory you upload to |
| 54 | +your server. (See [Application Builds](#application-builds) below.) |
| 55 | + |
| 56 | +Run the server file: |
| 57 | + |
| 58 | +``` |
| 59 | +$ PORT=8000 node fastboot-server.js |
| 60 | +``` |
| 61 | + |
| 62 | +This will start an HTTP server on port 8000. To stop the server, type |
| 63 | +`Ctrl-C`. |
| 64 | + |
| 65 | +NOTE: If you want to continue running `ember serve` in development, name the file `fastboot-server.js` instead. |
| 66 | + |
| 67 | +## Application Builds |
| 68 | + |
| 69 | +When you build an Ember.js app via `ember build`, it will build the app |
| 70 | +for production and, by default, put the resulting files in your |
| 71 | +application's `dist` directory. |
| 72 | + |
| 73 | +## Clustering |
| 74 | + |
| 75 | +Because Node.js is single-threaded, you must run multiple processes to |
| 76 | +take advantage of multi-core systems. FastBoot App Server takes |
| 77 | +advantage of Node's clustering support out of the box, automatically |
| 78 | +spawning one worker HTTP server per core. You can override this via `options.workerCount`. |
| 79 | + |
| 80 | +The app server will automatically spawn a new worker if one dies while |
| 81 | +handling a request. When a new application deploy is detected, workers |
| 82 | +will automatically reload with the newest version. |
| 83 | + |
| 84 | +## Custom HTTP Server |
| 85 | +You can customize HTTP server (add middlewares, subdomains, etc.), either directly: |
| 86 | +```js |
| 87 | +// start.js |
| 88 | +const FastBootAppServer = require('fastboot-app-server'); |
| 89 | +const ExpressHTTPServer = require('fastboot-app-server/src/express-http-server'); |
| 90 | + |
| 91 | +const httpServer = new ExpressHTTPServer(/* {options} */); |
| 92 | +const app = httpServer.app; |
| 93 | +app.use('/api', apiRoutes); |
| 94 | +let server = new FastBootAppServer({ |
| 95 | + httpServer: httpServer |
| 96 | +}); |
| 97 | + |
| 98 | +server.start(); |
| 99 | +``` |
| 100 | +or extend the provided HTTP server and override any methods you need: |
| 101 | +```js |
| 102 | +// my-custom-express-server.js |
| 103 | +const FastBootAppServer = require('fastboot-app-server'); |
| 104 | +const ExpressHTTPServer = require('fastboot-app-server/src/express-http-server'); |
| 105 | + |
| 106 | +class MyCustomExpressServer extends ExpressHTTPServer { |
| 107 | + serve(middleware) { |
| 108 | + // put your custom code here, don't forget to add fastboot etc. |
| 109 | + } |
| 110 | +} |
| 111 | +// start.js |
| 112 | +const MyCustomExpressServer = require('./my-custom-express-server'); |
| 113 | +const httpServer = new MyCustomExpressServer(/* {options} */); |
| 114 | +let server = new FastBootAppServer({ |
| 115 | + httpServer: httpServer |
| 116 | +}); |
| 117 | + |
| 118 | +server.start(); |
| 119 | +``` |
| 120 | + |
| 121 | +## Pre and Post FastBoot middleware hooks |
| 122 | + |
| 123 | +If you need something less than a custom server and just want to run some middleware |
| 124 | +before or after FastBoot runs, the server provides hooks for you to do so: |
| 125 | + |
| 126 | +```js |
| 127 | +// Custom Middlewares |
| 128 | +function modifyRequest(req, res, next) { /* do pre-fastboot stuff to `req` */ }; |
| 129 | +function handleErrors(err, req, res, next) { /* do error recovery stuff */ }; |
| 130 | + |
| 131 | +const server = FastBootAppServer({ |
| 132 | + beforeMiddleware: function (app) { app.use(modifyRequest); }, |
| 133 | + afterMiddleware: function (app) { app.use(handleErrors); } |
| 134 | +}) |
| 135 | +``` |
| 136 | + |
| 137 | +## Downloaders |
| 138 | + |
| 139 | +You can point the app server at a static path that you manage, but that |
| 140 | +means taking responsibility for uploading builds to each server |
| 141 | +whenever you want to deploy a new version. |
| 142 | + |
| 143 | +Instead, you can provide the app server with a _downloader_, an adapter |
| 144 | +that knows how to download the current version of your application. |
| 145 | + |
| 146 | +For example, to use the S3 downloader that downloads a zip file from |
| 147 | +AWS S3: |
| 148 | + |
| 149 | +```js |
| 150 | +const S3Downloader = require('fastboot-s3-downloader'); |
| 151 | +const FastBootAppServer = require('fastboot-app-server'); |
| 152 | + |
| 153 | +let downloader = new S3Downloader({ |
| 154 | + bucket: 'S3_BUCKET', |
| 155 | + key: 'S3_KEY' |
| 156 | +}); |
| 157 | + |
| 158 | +let server = new FastBootAppServer({ |
| 159 | + downloader: downloader |
| 160 | +}); |
| 161 | + |
| 162 | +server.start(); |
| 163 | +``` |
| 164 | + |
| 165 | +### Available Downloaders |
| 166 | + |
| 167 | +* [fastboot-s3-downloader](https://github.com/ember-fastboot/fastboot-s3-downloader) |
| 168 | +* [fastboot-gcloud-storage-downloader](https://github.com/EmberSherpa/fastboot-gcloud-storage-downloader) |
| 169 | + |
| 170 | +### Writing a Downloader |
| 171 | + |
| 172 | +To write your own downloader, construct an object that conforms to the |
| 173 | +following interface: |
| 174 | + |
| 175 | +#### `download()` |
| 176 | + |
| 177 | +Returns a promise that resolves to the path to the downloaded `dist` |
| 178 | +directory (which does not have to be named `dist`). |
| 179 | + |
| 180 | +Note that `download()` may be called more than once in the lifetime of |
| 181 | +an application, if a new version is deployed. Make sure your downloader |
| 182 | +cleans up after itself to avoid running out of disk space. |
| 183 | + |
| 184 | +## Notifiers |
| 185 | + |
| 186 | +Once the FastBoot App Server is up and running, it will happily chug |
| 187 | +away until the server dies or it reaches the inevitable heat death of the |
| 188 | +universe. Before that happens, presumably, you may want to deploy a new |
| 189 | +version of your application. |
| 190 | + |
| 191 | +_Notifiers_ are responsible for detecting when a new version of an app |
| 192 | +has been deployed and reloading the app server. |
| 193 | + |
| 194 | +For example, here's how to use the S3 notifier, which polls the last |
| 195 | +modified date of a file on S3 to detect new versions: |
| 196 | + |
| 197 | +```js |
| 198 | +const S3Notifier = require('fastboot-s3-notifier'); |
| 199 | +const FastBootAppServer = require('fastboot-app-server'); |
| 200 | + |
| 201 | +let notifier = new S3Notifier({ |
| 202 | + bucket: S3_BUCKET, |
| 203 | + key: S3_KEY |
| 204 | +}); |
| 205 | + |
| 206 | +let server = new FastBootAppServer({ |
| 207 | + notifier: notifier |
| 208 | +}); |
| 209 | + |
| 210 | +server.start(); |
| 211 | +``` |
| 212 | + |
| 213 | +### Available Notifiers |
| 214 | + |
| 215 | +* [fastboot-s3-notifier](https://github.com/ember-fastboot/fastboot-s3-notifier) |
| 216 | +* [fastboot-fs-notifier](https://github.com/iheanyi/fastboot-fs-notifier) |
| 217 | +* [fastboot-watch-notifier](https://github.com/pwfisher/fastboot-watch-notifier) |
| 218 | +* [fastboot-gcloud-storage-notifier](https://github.com/EmberSherpa/fastboot-gcloud-storage-notifier) |
| 219 | + |
| 220 | +### Writing a Notifier |
| 221 | + |
| 222 | +To write your own notifier, construct an object that conforms to the |
| 223 | +following interface: |
| 224 | + |
| 225 | +#### `subscribe(notify)` |
| 226 | + |
| 227 | +The `subscribe()` method on your notifier is passed a `notify` function. |
| 228 | +If you detect that a new version of your app has been deployed (whether |
| 229 | +via polling or a push notification), call this function to trigger a |
| 230 | +reload. |
| 231 | + |
| 232 | +## Basic Authentication |
| 233 | + |
| 234 | +You can enable Basic Authentication by providing `username` and `password` options: |
| 235 | + |
| 236 | +```js |
| 237 | +const FastBootAppServer = require('fastboot-app-server'); |
| 238 | + |
| 239 | +let server = new FastBootAppServer({ |
| 240 | + username: 'tomster', |
| 241 | + password: 'zoey' |
| 242 | +}); |
| 243 | +``` |
| 244 | + |
| 245 | +## Scraper Issues |
| 246 | + |
| 247 | +### Twitter and LinkedIn |
| 248 | + |
| 249 | +As of 2019-06-06, Twitter and LinkedIn's scrapers have a hard time extracting your site's metadata for sharing if `chunkedResponse` is set to `true` in your `server.js` file. Set `chunkedResponse: false` if your meta tags are in place but the [Twitter card validator](https://cards-dev.twitter.com/validator) shows "Card not found" or [LinkedIn's Post Inspector](https://www.linkedin.com/post-inspector/) shows a 500 error. |
0 commit comments