Skip to content

Instantly share code, notes, and snippets.

@rhythnic
Created May 20, 2019 17:25
Show Gist options
  • Save rhythnic/3d63d8537c592e04a8a62a99c8bc0ca2 to your computer and use it in GitHub Desktop.
Save rhythnic/3d63d8537c592e04a8a62a99c8bc0ca2 to your computer and use it in GitHub Desktop.
Singe Page App Express Utility
// Serve a single page application (web-client)
const path = require('path')
const Express = require('express')
module.exports = function serveSpa ({ app, omit, public }) {
if (!omit) omit = () => false
app
.use(Express.static(public))
.get('*', (req, res, next) => {
if (req.accepts('html') && !omit(req)) {
return res.sendFile(path.join(public, 'index.html'))
}
return next()
})
}
// example code
// const app = Express()
// serveSpa({
// app,
// public: path.join(__dirname, '../public'),
// omit: req => /graphql/.test(req.url)
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment