4.1.2 • Published 5 months ago

fastboot-express-middleware v4.1.2

Weekly downloads
17,101
License
MIT
Repository
github
Last release
5 months ago

FastBoot Express Middleware

Greenkeeper badge

Build Status

This middleware is a small wrapper around the fastboot package, which renders Ember.js apps in Node.js.

By adding this middleware to your Express app, you can serve HTML from a rendered Ember.js app to clients that don't support JavaScript, such as curl, search crawlers, or users with JavaScript disabled.

Note that this is just an Express middleware and there is more needed to serve apps in a production environment. If you want to server-side rendered Ember applications without doing a lot of work, you are recommended to consider the FastBoot App Server, which manages many of the hard parts for you.

That said, this middleware is designed to be easy to integrate for those who already have existing Express stacks, or who want maximum flexibility in how requests are handled.

Usage

const express = require('express');
const fastbootMiddleware = require('fastboot-express-middleware');

let app = express();

app.get('/*', fastbootMiddleware('/path/to/dist'));

app.listen(3000, function () {
  console.log('FastBoot app listening on port 3000!');
});

Building Your Ember App

Before you can use your app with FastBoot, you must first install the ember-cli-fastboot addon and build your app by running ember build. The build process will compile your app into a version that is compatible with both Node.js and the browser and put it in the dist directory. This dist directory is the path you should provide to the middleware to specify which Ember app to load and render.

Resilient Mode

By default, errors during render will cause the middleware to send an HTTP 500 status code as the response. In order to swallow errors and return a 200 status code with an empty HTML page, set the resilient flag to true:

app.get('/*', fastbootMiddleware({
  distPath: '/path/to/dist',
  resilient: true
}));

Resilient mode still calls next(err) to propagate your error to any subsequent middleware that you apply after this one. You can use this feature to track errors or log analytics.

However, because FastBoot is reslient still sends the response to the client. You cannot alter the response with any of your post-fastboot middleware.

Custom FastBoot Instance

For more control over the FastBoot instance that is created to render the Ember app, you can pass a custom instance that the middleware will use instead of creating its own:

let fastboot = new FastBoot({
  distPath: 'path/to/dist'
});

let middleware = fastbootMiddleware({
  fastboot: fastboot
});

app.get('/*', middleware);

// ...later
fastboot.reload();

Response chunking

By default, the middleware writes the complete response at once but response chunking (aka HTTP Streaming) is available via a config switch:

app.get('/*', fastbootMiddleware({
  distPath: '/path/to/dist',
  chunkedResponse: true
}));

Enabling response chunking will result in the response being delivered in multiple chunks (one for the head, one for the body and one for each shoebox) which helps getting the HTML to clients faster.

VisitOptions

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.

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.

app.get('/*', fastbootMiddleware({
  distPath: '/path/to/dist',
  visitOptions: {
    metadata: {
      foo: 'bar'
    }
  }
}));

Tests

npm test

4.1.2

5 months ago

4.1.1

12 months ago

4.1.0

1 year ago

4.0.0

1 year ago

3.3.1

2 years ago

3.3.2

2 years ago

3.3.0

2 years ago

3.2.0

2 years ago

3.2.0-beta.5

2 years ago

3.2.0-beta.4

3 years ago

3.2.0-beta.3

3 years ago

3.2.0-beta.2

3 years ago

3.2.0-beta.1

3 years ago

3.1.0

3 years ago

3.0.0

4 years ago

2.0.0

5 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

1.0.0-rc.8

7 years ago

1.0.0-rc.7

7 years ago

1.0.0-rc.6

7 years ago

1.0.0-rc.4

8 years ago

1.0.0-rc.3

8 years ago

1.0.0-rc.2

8 years ago

1.0.0-rc.1

8 years ago

1.0.0-rc.0

8 years ago

1.0.0-beta.8

8 years ago

1.0.0-beta.7

8 years ago

1.0.0-beta.6

8 years ago

1.0.0-beta.5

8 years ago

1.0.0-beta.4

8 years ago

1.0.0-beta.3

8 years ago

1.0.0-beta.2

8 years ago

1.0.0-beta.1

8 years ago

0.1.0

8 years ago