# express-history-api-fallback

> Simple fallback for Express-served single page apps that use the HTML5 History API for client side routing.

Latest version **2.2.1** (published 2017-05-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-history-api-fallback
pnpm add express-history-api-fallback
yarn add express-history-api-fallback
bun add express-history-api-fallback
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2017-05-17 |
| First published | 2015-10-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sebastiaan Deckers |
| Maintainers | seb |
| Keywords | express, html5, history api, fallback, spa, middleware, connect, pushstate, popstate, single page app, router, client side |

## Links

- npm: https://www.npmjs.com/package/express-history-api-fallback
- Repository: https://gitlab.com/sebdeckers/express-history-api-fallback
- Homepage: https://gitlab.com/sebdeckers/express-history-api-fallback#readme
- Issues: https://gitlab.com/sebdeckers/express-history-api-fallback/issues
- npm.io page: https://npm.io/package/express-history-api-fallback

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 2.2.1 (latest) — 2017-05-17
- 2.2.0 — 2017-05-17
- 2.1.0 — 2016-12-15
- 2.0.0 — 2015-10-26
- 1.0.0 — 2015-10-26

## README

# express-history-api-fallback
A tiny, accurate, fast Express middleware for single page apps with client side routing.

[![Build Status](https://travis-ci.org/cbas/express-history-api-fallback.svg?branch=master)](https://travis-ci.org/cbas/express-history-api-fallback)
[![codecov.io](https://codecov.io/github/cbas/express-history-api-fallback/coverage.svg?branch=master)](https://codecov.io/github/cbas/express-history-api-fallback?branch=master)

[![NPM](https://nodei.co/npm/express-history-api-fallback.png)](https://www.npmjs.com/package/express-history-api-fallback)

Works as a middleware for Express. Can be used as either an application middleware or a router middleware.

```js
import fallback from 'express-history-api-fallback'
import express from 'express'
const app = express()
const root = `${__dirname}/public`
app.use(express.static(root))
app.use(fallback('index.html', { root }))
```
Or in ECMAScript 5:
```js
var fallback = require('express-history-api-fallback')
var express = require('express')
var app = express()
var root = __dirname + '/public'
app.use(express.static(root))
app.use(fallback('index.html', { root: root }))
```

## fallback(path[, options])
Returns a middleware for use by Express applications and routers.

Arguments are passed to [res.sendFile()](http://expressjs.com/api.html#res.sendFile) in `express@>=v4.8.0`, or [res.sendfile()](http://expressjs.com/en/3x/api.html#res.sendfile) otherwise.

Absolute path:
```js
app.use(fallback(__dirname + '/dist/app.html'))
```
Relative path:
```js
app.use(fallback('dist/app.html', { root: __dirname }))
```

### path
Location of the HTML file containing single page app entry point.

Unless the `root` option is set in the `options` object, `path` must be an absolute path of the file.

### options
Valid options are `maxAge`, `root`, `lastModified`, `headers`, and `dotfiles`. See [Response.sendFile()](http://expressjs.com/api.html#res.sendFile) for details. Note that only `maxAge` and `root` are supported with `express@<4.8`.

## 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 `index.html` for `POST` or 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 `res.sendFile()` in Express `>=4.8.0` do 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"](https://ninja.sg/spa-router-fallback/) for an overview of the problems with alternative middlewares.

---
_Source: https://npm.io/package/express-history-api-fallback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
