FastBoot Express Middleware
This package provides an Express.js middleware for rendering Ember.js applications on the server side using FastBoot. It allows developers to integrate server-side rendering (SSR) capabilities into existing Express.js applications, primarily for SEO, improved initial page load performance, and serving non-JavaScript clients. The current stable version is 4.1.2. While there isn't a strict public release cadence, it typically follows major Ember/FastBoot updates and critical bug fixes, with `v3.0.0` introducing significant breaking changes. Its key differentiator is its direct integration with Express, offering maximum flexibility for users who prefer to manage their own server stack, in contrast to the more opinionated `FastBoot App Server`. It's designed for scenarios where an existing Express server needs to incorporate Ember FastBoot rendering without adopting a full FastBoot-specific server solution.
Common errors
-
Error: Cannot find module 'fastboot-express-middleware'
cause The package `fastboot-express-middleware` is not installed or not resolvable in your project.fixRun `npm install fastboot-express-middleware` or `yarn add fastboot-express-middleware` in your project directory. -
ENOENT: no such file or directory, stat '/path/to/dist'
cause The `distPath` provided to the `fastbootMiddleware` function does not point to a valid Ember application build directory, or the Ember application has not been built yet.fixEnsure your Ember application has `ember-cli-fastboot` installed and run `ember build`. Then, verify that the `distPath` option in your middleware configuration correctly points to the `dist` folder of your built Ember app. -
TypeError: app.get is not a function
cause This error indicates that `app` is not an Express application instance, or that `express` was not properly imported/initialized.fixEnsure you have `const express = require('express');` and `let app = express();` correctly set up before using `app.get` or `app.use`.
Warnings
- breaking Version 3.0.0 dropped support for Node.js 4, 6, 8, 9, 11, and 13. Ensure your Node.js environment is 12.*, 14.*, or >=16.
- breaking Version 3.0.0 updated its internal `fastboot` dependency to `fastboot@3.1.0`. This might introduce breaking changes from the underlying `fastboot` package itself if your Ember app relies on specific older FastBoot behaviors or APIs.
- gotcha When `resilient: true` is enabled, errors during rendering will result in an HTTP 200 status code and an empty HTML page. While subsequent error handling middleware will still be called via `next(err)`, you *cannot* alter the HTTP response within these post-FastBoot middleware because FastBoot has already sent a response.
- gotcha For the middleware to function correctly, your Ember application must be built using `ember-cli-fastboot` and the `distPath` option must point to the resulting `dist` directory. This is not a runtime dependency but a prerequisite for your Ember app.
Install
-
npm install fastboot-express-middleware -
yarn add fastboot-express-middleware -
pnpm add fastboot-express-middleware
Imports
- fastbootMiddleware
import fastbootMiddleware from 'fastboot-express-middleware';
const fastbootMiddleware = require('fastboot-express-middleware'); - FastBoot
const FastBoot = require('fastboot');
Quickstart
const express = require('express');
const fastbootMiddleware = require('fastboot-express-middleware');
const path = require('path');
// IMPORTANT: Replace '/path/to/your/ember-app/dist' with the actual path
// after running 'ember build'. For example, if your Express app is in the
// root and your Ember app is in a subdirectory named 'my-ember-app',
// this path might be path.join(__dirname, '..', 'my-ember-app', 'dist');
const distPath = process.env.EMBER_APP_DIST_PATH || path.join(__dirname, 'dist');
let app = express();
// Serve static assets from the Ember app's 'dist' folder
app.use(express.static(distPath));
// Apply the FastBoot middleware for all routes
app.get('/*', fastbootMiddleware(distPath));
const PORT = process.env.PORT || 3000;
app.listen(PORT, function () {
console.log(`FastBoot app listening on port ${PORT}! Serving from: ${distPath}`);
});