server.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. var express = require('express');
  15. var fs = require('fs')
  16. // Create our app
  17. var app = express();
  18. var PORT = process.env.PORT || 3000;
  19. // Write file to disk with process env variables, so that the client code can read
  20. if (!fs.existsSync('./dist')) {
  21. fs.mkdirSync('./dist');
  22. }
  23. fs.writeFileSync('./dist/env.js', 'window.env = { CORIOLIS_URL: "' + (process.env.CORIOLIS_URL || '/') + '" }')
  24. let isDev = process.argv.find(a => a === '--dev')
  25. if (isDev) {
  26. let isBrowserOpen = false
  27. var webpack = require('webpack');
  28. var webpackConfig = require('./webpack.config');
  29. var compiler = webpack(webpackConfig);
  30. app.use(require("webpack-dev-middleware")(compiler, {
  31. noInfo: false,
  32. publicPath: webpackConfig.output.publicPath,
  33. stats: {
  34. colors: true
  35. },
  36. log: function (text) {
  37. let statusIndex = text.indexOf('webpack: Compiled') > -1
  38. ? text.indexOf('webpack: Compiled') : text.indexOf('webpack: Failed')
  39. if (statusIndex > -1) {
  40. let left = text.substr(0, statusIndex)
  41. let isSuccesfull = text.indexOf('webpack: Compiled successfully.') > -1
  42. let color = text.indexOf('webpack: Compiled with warnings.') > -1 ? '\033[43m\033[30m' : ''
  43. color = isSuccesfull ? '\033[42m\033[30m' : color
  44. color = text.indexOf('webpack: Failed to compile.') > -1 ? '\033[41m' : color
  45. let end = color + text.substr(statusIndex) + '\033[0m'
  46. console.log(left + end)
  47. if (!isBrowserOpen && isSuccesfull) {
  48. isBrowserOpen = true
  49. console.log('\033[96mServer is available at http://localhost:' + PORT + '\033[0m')
  50. }
  51. } else {
  52. console.log(text)
  53. }
  54. }
  55. }));
  56. app.use(require("webpack-hot-middleware")(compiler, {
  57. log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
  58. }));
  59. }
  60. app.use(express.static('dist'));
  61. app.use(function (req, res, next) {
  62. res.redirect(req.baseUrl + '/#' + req.url)
  63. });
  64. app.listen(PORT, function () {
  65. console.log('Express server is up on port ' + PORT);
  66. });