A tiny, accurate, fast Express middleware for single page apps with client side routing.
Works as a middleware for Express. Can be used as either an application middleware or a router middleware.
const app =const root = `/public`appapp
Or in ECMAScript 5:
var fallback =var express =var app =var root = __dirname + '/public'appapp
Returns a middleware for use by Express applications and routers.
Location of the HTML file containing single page app entry point.
root option is set in the
path must be an absolute path of the file.
Valid options are
dotfiles. See Response.sendFile() for details. Note that only
root are supported with
But doesn't this already exist?
Yes, but this implementation is much better.
- Only for GET (and HEAD) requests: The fallback should not serve your
POSTor other requests.
- Only for HTML requests: Never serve mistakenly for JS or CSS or image or other static file requests. Less debugging headaches.
- Only when needed: Serve the fallback only when the file is missing.
- High performance: Let
>=4.8.0do the heavy lifting of serving the file.
- Minimal code: Just a few lines. No magic. No complexity.
See the blog post "Single Page App Routing with Express & Node.js" for an overview of the problems with alternative middlewares.