'use strict';
const express = require ('express');
const bodyParser = require ('body-parser');
const cookieParser = require ('cookie-parser');
const expressSession = require ('express-session');
const path = require ('path');
const passport = require ('passport');
const auth = require ('./auth');
const routes = require ('./routes');

// the secret for the session, should be set in an environment variable
// some random text used as a placeholder for dev
const sessionSecret = process.env.SESSION_SECRET || 'randomtext_aseroja';

// ensure HTTPS is used for all interactions
let httpsOnly = (req, res, next) => {
  if (req.headers['x-forwarded-proto'] &&
    req.headers['x-forwarded-proto'] !== 'https') {
    return res.redirect (['https://', req.hostname, req.url].join (''));
  }
  next ();
};

// Start the server.
function start (port, db, yelp) {
  return new Promise ((resolve, reject) => {
    console.log ('Starting server');
    Promise.resolve ().then (() => {
      // set up static HTML serving
      let app = express ();

      if (process.env.NODE_ENV === 'production') {
        app.use (httpsOnly);
      }

      // set up HTTP parsers and session manager
      app.use (cookieParser ());
      app.use (bodyParser.json ());
      app.use (bodyParser.urlencoded ({extended:true}));
      app.use (expressSession ({
        secret: sessionSecret,
        saveUninitialized: true,
        resave: true
      }));

      // set up passport authentication, attach to express session manager
      auth.init (db);
      app.use (passport.initialize ());
      app.use (passport.session ());

      // create server with HTML and REST routes
      routes.init (app, db, yelp);

      app.get ('*.js', (req, res) => {
        res.set ({
          'content-type': 'text/javascript',
          'content-encoding': 'gzip'
        });
        res.sendFile (path.join (__dirname, 'public' + req.path + '.gz'));
      });

      // static file handling
      app.use (express.static (path.join (__dirname, 'public')));

      // for not explicitly handled REST routes, return 404 message
      app.use ('/api/*', (req, res) => {
        res.status (404).json ({});
      });
      // for all other routes, let client react-router handle them
      app.get ('*', (req, res) => {
        res.sendFile (path.join (__dirname, 'public/index.html'));
      });

      app.listen (port, () => {
        console.log ('Server listening on port ' + port);
        resolve ();
      });
    }).catch (err => {
      reject (err);
    });
  });
}

exports.start = start;

@@ -184,24 +184,51 @@
 -session');%0A
+const fs = require ('fs');%0A
 const path =
@@ -1746,32 +1746,141 @@
 (req, res) =%3E %7B%0A
+        let file = path.join (__dirname, 'public' + req.path + '.gz');%0A        if (fs.existsSync (file)) %7B%0A  
         res.set 
@@ -1878,24 +1878,26 @@
  res.set (%7B%0A
+  
           'c
@@ -1931,32 +1931,34 @@
 ipt',%0A          
+  
 'content-encodin
@@ -1972,28 +1972,161 @@
 ip'%0A        
+  
 %7D);%0A
+          res.sendFile (file);%0A        %7D else %7B%0A          res.set (%7B%0A            'content-type': 'text/javascript'%0A          %7D);%0A  
         res.
@@ -2180,19 +2180,21 @@
 path
- + '.gz'));
+));%0A        %7D
 %0A   

update js delivery