text: const express = require("express");
  const mongoose = require("mongoose");
  const path = require("path");
  const logger = require("morgan");
  const Workout = require("./models/index.js");
  
  const PORT = process.env.PORT || 3000
  
  const app = express();
  const db = require("./models");
  
  app.use(express.urlencoded({ extended: true }));
  app.use(express.json());
  
  app.use(express.static("public"));
  app.use(logger("dev"));
  
  mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/workout", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false
  });
  
  app.get("/exercise", (req, res) => {
    res.sendFile(path.join(__dirname, "./public/exercise.html"));
  });
  
  app.get("/stats", (req, res) => {
    res.sendFile(path.join(__dirname, "./public/stats.html"));
  });
  
  
  
  app.post("/api/workouts", (req, res) => {
    Workout.create({})
      .then(dbWorkout => {
        res.json(dbWorkout);
      })
      .catch(err => {
        res.json(err);
      });
  });
  
  app.put("/api/workouts/:id", (req, res) => {
    Workout.findByIdAndUpdate(req.params.id, { $push: { exercises: req.body } }, { new: true, runValidators: true })
      .then(dbWorkout => {
        console.log(dbWorkout)
        res.json(dbWorkout);
      })
      .catch(err => {
        res.json(err)
      })
  });
  
  app.get("/api/workouts", (req, res) => {
    Workout.aggregate([
      {
        $addFields: {
          totalDuration: { $sum: '$exercises.duration' }
        }
      }
    ])
      .then(result => {
        res.json(result)
      })
      .catch(err => {
        console.log(err)
      })
  });
  
  app.get("/api/workouts/range", (req, res) => {
    Workout.aggregate([
      {
        $addFields: {
          totalDuration: { $sum: '$exercises.duration' }
        }
      }
    ])
      .sort({ day: -1 })
      .limit(7)
      .then(result => {
        res.json(result)
      })
      .catch(err => {
        console.log(err)
      })
  });
  
  
  
  
  app.listen(PORT, () => {
    console.log(`App running on port ${PORT}!`);
  });
  
  
  module.exports = db;