Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Express SPA history API express.static fallback

Mostly taken from: https://gitlab.com/sebdeckers/express-history-api-fallback, without all the repo/package fluff.

let staticPath = '/path/to/dist'

app.use(express.static(staticPath))

app.use('/', ((...args) => (req, res, next) => {
  if ((req.method === 'GET' || req.method === 'HEAD') && req.accepts('html')) {
    (res.sendFile || res.sendfile).call(res, ...args, err => err && next())
  } else next()
})('index.html', {
  root: staticPath
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.