text: const express = require('express');
  const cookieParser = require('cookie-parser');
  const morgan = require('morgan');
  const path = require('path');
  const session = require('express-session');
  const flash = require('connect-flash');
  const passport = require('passport');
  const helmet = require('helmet');
  require('dotenv').config();
  
  const indexRouter = require('./routes/index');
  const apiRouter = require('./routes/api');
  const authRouter = require('./routes/auth');
  const usersRouter = require('./routes/users');
  
  const {sequelize} = require('./models');
  const passportConfig = require('./passport');
  
  const app = express();
  sequelize.sync({
      force: process.env.DB_FORCE_SYNC === "true"
  });
  
  passportConfig(passport);
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  app.set('port', process.env.PORT || 8001);
  
  app.use(helmet());
  app.use(morgan('dev'));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(express.json());
  app.use(express.urlencoded({extended: false}));
  app.use(cookieParser(process.env.COOKIE_SECRET));
  app.use(session({
      resave: true,
      saveUninitialized: true,
      secret: process.env.COOKIE_SECRET,
      cookie: {
          httpOnly: true,
          secure: false,
      },
  }));
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  
  app.disable('x-powered-by');
  
  app.use('/', indexRouter);
  app.use('/api', apiRouter);
  app.use('/auth', authRouter);
  app.use('/users', usersRouter);
  
  // catch 404 and forward to error handler
  app.use((req, res, next) => {
      const err = new Error('Not Found');
      err.status = 404;
      next(err);
  });
  
  // error handler
  app.use((err, req, res, next) => {
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};
      res.status(err.status || 500);
      res.render('error', {
          title: "Error",
          user: req.user,
          loginError: req.flash('loginError'),
      });
  });
  
  app.listen(app.get('port'), () => {
      console.log(`[${process.env.NODE_ENV}] Listening on port ${app.get('port')}`);
  });
  
  module.exports = app;
  