Browse Source

L11: production-ish Express server.

Frederic G. MARAND 8 years ago
parent
commit
b9140ac918
7 changed files with 1665 additions and 11 deletions
  1. 1 0
      .gitignore
  2. 0 3
      index.css
  3. 15 5
      package.json
  4. 1619 0
      public/index.css
  5. 0 0
      public/index.html
  6. 19 0
      server.js
  7. 11 3
      webpack.config.js

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 /.idea/
 /node_modules/
+/npm-debug.log
 bundle.js
 *.bundle.js

+ 0 - 3
index.css

@@ -1,3 +0,0 @@
-a.active {
-  color: red;
-}

+ 15 - 5
package.json

@@ -1,14 +1,24 @@
 {
-  "name": "tutorial",
-  "version": "1.0.0",
+  "author": "",
   "description": "",
+  "license": "ISC",
   "main": "index.js",
+  "name": "tutorial",
+  "repository": {
+    "type": "git",
+    "url": "http://formation.osinet.fr:3000/fgm/react-router-tutorial.git"
+  },
   "scripts": {
-    "start": "webpack-dev-server --inline --content-base . --history-api-fallback"
+    "start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
+    "start:dev": "webpack-dev-server --inline --content-base public --history-api-fallback",
+    "start:prod": "webpack && node server.js"
   },
-  "author": "",
-  "license": "ISC",
+  "version": "1.0.0",
+
   "dependencies": {
+    "compression": "^1.6.1",
+    "express": "^4.13.4",
+    "if-env": "^1.0.0",
     "react": "^0.14.7",
     "react-dom": "^0.14.7",
     "react-router": "^2.0.0"

File diff suppressed because it is too large
+ 1619 - 0
public/index.css


+ 0 - 0
index.html → public/index.html


+ 19 - 0
server.js

@@ -0,0 +1,19 @@
+var express = require('express');
+var path = require('path');
+var compression = require('compression');
+
+var app = express();
+app.use(compression());
+
+// Server our static stuff like index.css
+app.use(express.static(path.join(__dirname, 'public')));
+
+// Send all requests to index.html so browserHistory in React Router works
+app.get('*', function (req, res) {
+  res.sendFile(path.join(__dirname, 'public', 'index.html'));
+});
+
+var PORT = process.env.PORT || 8080;
+app.listen(PORT, function () {
+  console.log('"Production" Express server running at localhost:' + PORT);
+});

+ 11 - 3
webpack.config.js

@@ -1,15 +1,23 @@
+var webpack = require('webpack');
+
 module.exports = {
   entry: './index.js',
 
   output: {
     filename: 'bundle.js',
-    publicPath: ''
+    path: 'public',
+    publicPath: 'public'
   },
 
   module: {
     loaders: [
       { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' }
     ]
-  }
-}
+  },
 
+  plugins: process.env.NODE_ENV === 'production' ? [
+    new webpack.optimize.DedupePlugin(),
+    new webpack.optimize.OccurenceOrderPlugin(),
+    new webpack.optimize.UglifyJsPlugin()
+  ] : []
+};

Some files were not shown because too many files changed in this diff