text: const express = require('express');
  const path = require('path');
  require('express-async-errors');
  const cors = require('cors');
  const mongoose = require('mongoose');
  const config = require('./utils/config');
  const logger = require('./utils/logger');
  const middleware = require('./utils/middleware');
  const blogsRouter = require('./controllers/blogs');
  const usersRouter = require('./controllers/users');
  const loginRouter = require('./controllers/login');
  
  const app = express();
  
  // Establish Database Connection
  logger.info('Connecting to', config.MONGODB_URI);
  
  // Connect to Database
  const connectToDb = async () => {
    try {
      await mongoose.connect(config.MONGODB_URI);
      logger.info('Connected to MongoDB');
    } catch (error) {
      logger.error('Error connecting to MongoDB: ', error.message);
    }
  };
  connectToDb();
  
  // Preprocessing
  app.use(cors());
  app.use(express.static('build'));
  app.use(express.json());
  app.use(middleware.requestLogger);
  app.use(middleware.tokenExtractor);
  
  // Route Handling
  app.use('/api/blogs', blogsRouter);
  app.use('/api/users', usersRouter);
  app.use('/api/login', loginRouter);
  
  // Front End
  app.use(express.static('public'));
  
  app.get('/health', (req, res) => {
    res.send('ok');
  });
  
  app.get('/version', (req, res) => {
    res.send('2');
  });
  
  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '/public/index.html'));
  });
  
  // Testing
  if (process.env.NODE_ENV === 'test') {
    // eslint-disable-next-line global-require
    const testingRouter = require('./controllers/testing');
    app.use('/api/testing', testingRouter);
  }
  
  // Error Handling
  app.use(middleware.unknownEndpoint);
  app.use(middleware.errorHandler);
  
  module.exports = app;
  