text: 'use strict';
  
  if (process.env.NODE_ENV !== 'production') {
    require('dotenv').config({ silent: true });
  }
  
  const express = require('express');
  const path = require('path');
  const port = process.env.PORT || 8000;
  
  const bodyParser = require('body-parser');
  const cookieParser = require('cookie-parser');
  const morgan = require('morgan');
  
  const app = express();
  const passport = require('passport');
  
  app.disable('x-powered-by');
  
  switch (app.get('env')) {
    case 'development':
      app.use(morgan('dev'));
      break;
  
    case 'production':
      app.use(morgan('short'));
      break;
  
    default:
  }
  
  app.use(passport.initialize());
  app.use(passport.session());
  
  passport.serializeUser((user, done) => {
    done(null, user);
  });
  
  passport.deserializeUser((obj, done) => {
    done(null, obj);
  });
  
  app.use(bodyParser.json());
  app.use(cookieParser());
  
  app.use(express.static(path.join(__dirname, 'public')));
  
  // CSRF protection
  app.use('/api', (req, res, next) => {
    if (/json/.test(req.get('Accept'))) {
      return next();
    }
  
    res.sendStatus(406);
  });
  
  const users = require('./routes/users');
  const auth = require('./routes/auth');
  const snippets = require('./routes/snippets');
  
  app.use('/api', users);
  app.use('/auth', auth);
  app.use('/api', snippets);
  
  app.use((_req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
  });
  
  app.use((_req, res) => {
    res.sendStatus(404);
  });
  
  // eslint-disable-next-line max-params
  app.use((err, _req, res, _next) => {
    if (err.status || (err.output && err.output.statusCode)) {
      return res
        .status(err.status || err.output.statusCode)
        .send(err);
    }
  
    // eslint-disable-next-line no-console
    console.error(err.stack);
    res.sendStatus(500);
  });
  
  app.listen(port, () => {
    if (process.env.NODE_ENV !== 'test') {
      // eslint-disable-next-line no-console
      console.log(`ON PORT ${port} LISTENING YOU ARE...`);
    }
  });
  
  module.exports = app;
  