Просмотр исходного кода

Merge pull request #115 from smiclea/server-es6

Refactor NodeJS Server ES5 code to ES6
Dorin Paslaru 8 лет назад
Родитель
Сommit
a28d79251e
4 измененных файлов с 98 добавлено и 63 удалено
  1. 1 0
      package.json
  2. 2 63
      server.js
  3. 54 0
      server/dev.js
  4. 41 0
      server/main.js

+ 1 - 0
package.json

@@ -58,6 +58,7 @@
     "babel-preset-env": "^1.6.0",
     "babel-preset-react": "^6.24.1",
     "babel-preset-stage-1": "^6.24.1",
+    "babel-register": "^6.26.0",
     "copyfiles": "^1.2.0",
     "cross-env": "^5.0.5",
     "express": "^4.16.1",

+ 2 - 63
server.js

@@ -12,66 +12,5 @@ You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-var express = require('express');
-var fs = require('fs')
-
-// Create our app
-var app = express();
-var PORT = process.env.PORT || 3000;
-
-// Write file to disk with process env variables, so that the client code can read
-if (!fs.existsSync('./dist')) {
-  fs.mkdirSync('./dist');
-}
-fs.writeFileSync('./dist/env.js', 'window.env = { CORIOLIS_URL: "' + (process.env.CORIOLIS_URL || '/') + '" }')
-
-let isDev = process.argv.find(a => a === '--dev')
-if (isDev) {
-  let isBrowserOpen = false
-  var webpack = require('webpack');
-  var webpackConfig = require('./webpack.config');
-  var compiler = webpack(webpackConfig);
-
-  app.use(require("webpack-dev-middleware")(compiler, {
-    noInfo: false,
-    publicPath: webpackConfig.output.publicPath,
-    stats: {
-      colors: true
-    },
-    log: function (text) {
-      let statusIndex = text.indexOf('webpack: Compiled') > -1
-        ? text.indexOf('webpack: Compiled') : text.indexOf('webpack: Failed')
-      if (statusIndex > -1) {
-        let left = text.substr(0, statusIndex)
-        let isSuccesfull = text.indexOf('webpack: Compiled successfully.') > -1
-        let color = text.indexOf('webpack: Compiled with warnings.') > -1 ? '\033[43m\033[30m' : ''
-        color = isSuccesfull ? '\033[42m\033[30m' : color
-        color = text.indexOf('webpack: Failed to compile.') > -1 ? '\033[41m' : color
-
-        let end = color + text.substr(statusIndex) + '\033[0m'
-        console.log(left + end)
-
-        if (!isBrowserOpen && isSuccesfull) {
-          isBrowserOpen = true
-          console.log('\033[96mServer is available at http://localhost:' + PORT + '\033[0m')
-        }
-      } else {
-        console.log(text)
-      }
-    } 
-  }));
-
-  app.use(require("webpack-hot-middleware")(compiler, {
-    log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
-  }));
-}
-
-app.use(express.static('dist'));
-
-app.use(function (req, res, next) {
-  res.redirect(req.baseUrl + '/#' + req.url)
-});
-
-app.listen(PORT, function () {
-  console.log('Express server is up on port ' + PORT);
-});
+require('babel-register')
+require('./server/main')

+ 54 - 0
server/dev.js

@@ -0,0 +1,54 @@
+/*
+Copyright (C) 2017  Cloudbase Solutions SRL
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import webpack from 'webpack'
+import webpackConfig from '../webpack.config'
+
+module.exports = (app, PORT) => {
+  let isBrowserOpen = false
+  const compiler = webpack(webpackConfig)
+
+  app.use(require('webpack-dev-middleware')(compiler, {
+    noInfo: false,
+    publicPath: webpackConfig.output.publicPath,
+    stats: {
+      colors: true,
+    },
+    log: text => {
+      let statusIndex = text.indexOf('webpack: Compiled') > -1
+        ? text.indexOf('webpack: Compiled') : text.indexOf('webpack: Failed')
+      if (statusIndex > -1) {
+        let left = text.substr(0, statusIndex)
+        let isSuccesfull = text.indexOf('webpack: Compiled successfully.') > -1
+        let color = text.indexOf('webpack: Compiled with warnings.') > -1 ? '\x1b[43m\x1b[30m' : ''
+        color = isSuccesfull ? '\x1b[42m\x1b[30m' : color
+        color = text.indexOf('webpack: Failed to compile.') > -1 ? '\x1b[41m' : color
+
+        let end = `${color + text.substr(statusIndex)}\x1b[0m`
+        console.log(left + end) // eslint-disable-line no-console
+
+        if (!isBrowserOpen && isSuccesfull) {
+          isBrowserOpen = true
+          console.log(`\x1b[96mServer is available at http://localhost:${PORT}\x1b[0m`) // eslint-disable-line no-console
+        }
+      } else {
+        console.log(text)// eslint-disable-line no-console
+      }
+    },
+  }))
+
+  app.use(require('webpack-hot-middleware')(compiler, {
+    log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000, // eslint-disable-line no-console
+  }))
+}

+ 41 - 0
server/main.js

@@ -0,0 +1,41 @@
+/*
+Copyright (C) 2017  Cloudbase Solutions SRL
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import express from 'express'
+import fs from 'fs'
+
+// Create our app
+const app = express()
+const PORT = process.env.PORT || 3000
+
+// Write file to disk with process env variables, so that the client code can read
+if (!fs.existsSync('./dist')) {
+  fs.mkdirSync('./dist')
+}
+fs.writeFileSync('./dist/env.js', `window.env = { CORIOLIS_URL: "${(process.env.CORIOLIS_URL || '/')}" }`)
+
+const isDev = process.argv.find(a => a === '--dev')
+if (isDev) {
+  require('./dev')(app, PORT)
+}
+
+app.use(express.static('dist'))
+
+app.use((req, res) => {
+  res.redirect(`${req.baseUrl}/#${req.url}`)
+})
+
+app.listen(PORT, () => {
+  console.log(`Express server is up on port ${PORT}`) // eslint-disable-line no-console
+})