diff --git a/lib/makeserver.js b/lib/makeserver.js index cfcdfe2a..5baba16b 100644 --- a/lib/makeserver.js +++ b/lib/makeserver.js @@ -147,6 +147,27 @@ module.exports = function(options) { var error500 = errorPage.error500(show500, options.wwwroot); var initPaths = options.settings.initPaths || []; + // Serve up prerendered pages if they exist for a given route + app.get('*', function (req, res, next) { + const decodedPath = decodeURIComponent(req.path); + const prerendered = path.resolve(options.wwwroot, `prerendered/${decodedPath}`, 'index.html'); + if (!prerendered.startsWith(path.resolve(options.wwwroot, 'prerendered/') + '/')) { + // Path traversal attack (using e.g. ..%2F) + next(); + return; + } + + if (fs.existsSync(prerendered)) { + res.sendFile(prerendered); + // Other /catalog/ routes should at least return index.html, in case of unresolved pathing/id + } else if (decodedPath.substr(0,9) === "/catalog/") { + res.sendFile(options.wwwroot + '/index.html'); + } else { + // For now, don't resolve remaining app urls SPA-style with all-routes-200-index.html + next(); + } + }); + if (serveWwwRoot) { initPaths.push(path.join(options.wwwroot, 'init')); } diff --git a/package.json b/package.json index 2eec9544..3005f04b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "terriajs-server", - "version": "3.3.4", + "version": "3.3.4-new-routing", "description": "NodeJS server for TerriaJS, consisting of a CORS proxy, proj4 CRS lookup service, ogr2ogr conversion service, and express static server.", "engineStrict": true, "engines": { @@ -44,12 +44,12 @@ "json5": "^2.1.0", "morgan": "^1.7.0", "pm2": "^3.2.2", - "terriajs-ogr2ogr": "^1.0.0", "proj4": "^2.3.12", "proj4js-defs": "0.0.1", "range_check": "^1.4.0", "request": "^2.67.0", "request-promise": "^4.0.1", + "terriajs-ogr2ogr": "^1.0.0", "when": "^3.7.7", "yargs": "^13.2.4" }, diff --git a/serverconfig.json.example b/serverconfig.json.example index 26f089d3..ad5a7eae 100644 --- a/serverconfig.json.example +++ b/serverconfig.json.example @@ -2,6 +2,10 @@ // Port to listen on. Overriden by the --port command line setting. port: 3001, + // If you are serving TerriaMap from a directory, specify the path to it here, + // otherwise leave it as '/' for root paths + baseHref: "/", + // List of domains which the server is willing to proxy for. Subdomains are included automatically. allowProxyFor : [ "nicta.com.au", diff --git a/terriajs-server.js b/terriajs-server.js old mode 100644 new mode 100755