import express from 'express';
import morgan from 'morgan';
import serverRenderer from './middleware/renderer';
const PORT = 3000;
const path = require('path');

// initialize the application and create the routes
const app = express();
const router = express.Router();

// Setup logger
app.use(morgan('combined'));

// root (/) should always serve our server rendered page
router.use('^/$', serverRenderer);

// other static resources should just be served as they are
router.use(express.static(
  path.resolve(__dirname, '..', 'build'),
  { maxAge: '30d' },
));

// tell the app to use the above rules
app.use(router);
// start the app
app.listen(PORT, (error) => {
  if (error) {
    return console.log('something bad happened', error); // eslint-disable-line no-console
  }

  console.log(`listening on ${PORT} ...`); // eslint-disable-line no-console

  return null;
});
