When server.js receives a static path it does not recognize, it serves up
index.html instead. This is useful when a user reloads a page in the SPA -- server.js does not know this path, but by serving up the SPA again the SPA knows how to handle that URL. This makes the app behave in a manner a user expects when reloading a page.
This commit is contained in:
parent
2c61988074
commit
54b911bcd4
@ -191,10 +191,25 @@ function main() {
|
|||||||
server.head(API_RE, proxy);
|
server.head(API_RE, proxy);
|
||||||
|
|
||||||
// where to serve static content from
|
// where to serve static content from
|
||||||
server.get(STATIC_RE, mod_restify.plugins.serveStatic({
|
let staticHandler = mod_restify.plugins.serveStatic({
|
||||||
directory: 'static',
|
directory: 'static',
|
||||||
default: 'index.html'
|
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
|
// enable HTTP server
|
||||||
server.listen(CONFIG.server.port, function listening() {
|
server.listen(CONFIG.server.port, function listening() {
|
||||||
|
Reference in New Issue
Block a user