Created
May 20, 2019 17:25
-
-
Save rhythnic/3d63d8537c592e04a8a62a99c8bc0ca2 to your computer and use it in GitHub Desktop.
Singe Page App Express Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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