diff --git a/bin/server.js b/bin/server.js index 75bd31b..6ddc839 100755 --- a/bin/server.js +++ b/bin/server.js @@ -191,10 +191,25 @@ function main() { server.head(API_RE, proxy); // where to serve static content from - server.get(STATIC_RE, mod_restify.plugins.serveStatic({ + let staticHandler = mod_restify.plugins.serveStatic({ directory: 'static', default: 'index.html' - })); + }); + server.get(STATIC_RE, function staticFunnel(req, res, next) { + staticHandler(req, res, function fileFound(err) { + // If we didn't find the requested static file, serve up the + // default (index.html) instead. This is useful when a user reloads + // some page in the SPA, since the path does not match anything the + // backend knows, but the SPA reloaded through index.html will know + // what to do with the original URL. + if (err && err.statusCode === 404) { + req.url = '/'; + staticHandler(req, res, next); + } else { + next(err); + } + }); + }); // enable HTTP server server.listen(CONFIG.server.port, function listening() {