import compression from 'compression';
import express from 'express';
import fs from 'fs';

const app = express();
const port = process.env.PORT || 8080;
app.use(compression());
app.enable('trust proxy');

app.use((req, res, next) => {
  if (req.secure || req.headers.host === `localhost:${port}`) {
    next();
  } else {
    res.redirect(`https://${req.headers.host}${req.url}`);
  }
});

app.use(express.static('./build'));

app.listen(port, (err) => {
  if (err) {
    console.log(err); // eslint-disable-line no-console
    return;
  }
  console.log(`App and API is live at http://localhost:${port}`); // eslint-disable-line no-console
});

import compression from 'compression';
import express from 'express';
import fs from 'fs';

const app = express();
const port = process.env.PORT || 8080;
app.use(compression());
app.enable('trust proxy');

app.use((req, res, next) => {
  if (req.secure || req.headers.host === `localhost:${port}`) {
    next();
  } else {
    res.redirect(`https://${req.headers.host}${req.url}`);
  }
});

app.use(express.static('./build'));

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});

app.listen(port, err => {
  if (err) {
    console.log(err); // eslint-disable-line no-console
    return;
  }
  console.log(`App and API is live at http://localhost:${port}`); // eslint-disable-line no-console
});

Add wildcard GET request to avoid 404 error when going to directly to a page or refreshing on a page.
Add wildcard GET request to avoid 404 error when going to directly to a page or refreshing on a page.
