From 68805d4e08d414386f81ad02b32a3b455c8137a6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 13 Jul 2018 10:50:57 +0300 Subject: [PATCH] cherry-pick features/react-routes --- lib/core/config.js | 4 +--- lib/modules/webserver/index.js | 1 + lib/modules/webserver/server.js | 26 ++++++++++++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/core/config.js b/lib/core/config.js index 3ca8fda6..71604a46 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -311,9 +311,7 @@ Config.prototype.loadCommunicationConfigFile = function() { Config.prototype.loadWebServerConfigFile = function() { var configObject = { - "enabled": true, - "host": defaultHost, - "port": 8000 + "enabled": true, "host": defaultHost, "port": 8000, "enableCatchAll": true }; let configFilePath = this._getFileOrOject(this.configDir, 'webserver', 'webserver'); diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 62748a2b..bd363b10 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -16,6 +16,7 @@ class WebServer { this.host = options.host || this.webServerConfig.host; this.port = options.port || this.webServerConfig.port; + this.enableCatchAll = this.webServerConfig.enableCatchAll === true; this.events.emit("status", __("Starting Server")); this.server = new Server({host: this.host, port: this.port}); diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index a91c9f96..ad136325 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -29,23 +29,29 @@ class Server { var app = express(); app.use(serve); - app.use('/embark', serveStatic(path.join(__dirname, 'backend'), {'backend': ['index.html', 'index.htm']})); + + app.use(express.static(path.join(fs.dappPath(this.dist)), {'index': ['index.html', 'index.htm']})); + app.use(['/embark', '/backend', '/admin'], express.static(path.join(__dirname, 'backend'), {'index': ['index.html', 'index.htm']})); // mount the sub app app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies expressWebSocket(app); - let apiCalls = self.plugins.getPluginsProperty("apiCalls", "apiCalls"); - console.dir(apiCalls); - for (let apiCall of apiCalls) { - console.dir("adding " + apiCall.method + " " + apiCall.endpoint); - app[apiCall.method].apply(app, [apiCall.endpoint, apiCall.cb]); - } + if (self.plugins) { + let apiCalls = self.plugins.getPluginsProperty("apiCalls", "apiCalls"); + for (let apiCall of apiCalls) { + console.dir("adding " + apiCall.method + " " + apiCall.endpoint); + app[apiCall.method].apply(app, [apiCall.endpoint, apiCall.cb]); + } + } - app.get('/embark', function (req, res) { - res.send('Welcome to Embark') - }); + if (this.enableCatchAll === true) { + app.get('/*', function (req, res) { + self.logger.trace('webserver> GET ' + req.path); + res.sendFile(path.join(fs.dappPath(self.dist, 'index.html'))); + } + } app.listen(this.port);