text: // ================ D: VALIDATION AND ESLINT ================== //
  
  require('dotenv').config();
  
  const express = require('express');
  const app = express();
  const cors = require('cors');
  const Note = require('./models/note');
  app.use(express.static('build'));
  app.use(express.json());
  app.use(cors());
  
  const requestLogger = (request, response, next) => {
    console.log('Method:', request.method);
    console.log('Path: ', request.path);
    console.log('Body: ', request.body);
    console.log('---');
    next();
  };
  
  app.use(requestLogger);
  
  app.get('/api/notes', (request, response) => {
    Note.find({}).then(notes => {
      response.json(notes);
    });
  });
  
  app.get('/api/notes/:id', (request, response, next) => {
    Note.findById(request.params.id)
      .then(note => {
        if (note) {
          response.json(note);
        } else {
          response.status(404).end();
        }
      })
      .catch(error => next(error));
  });
  
  app.delete('/api/notes/:id', (request, response, next) => {
    Note.findByIdAndRemove(request.params.id)
      .then(result => {
        response.status(204).end();
      })
      .catch(error => next(error));
  });
  
  app.post('/api/notes', (request, response, next) => {
    const body = request.body;
    // validity of note checked in route handler
    if (body.content === undefined) {
      return response.status(400).json({ error: 'content missing' });
    }
  
    const note = new Note({
      content: body.content,
      important: body.important || false,
      date: new Date(),
    });
  
    // note.save()
    //   .then(savedNote => {
    //     response.json(savedNote.toJSON());
    //   })
    //   .catch(error => next(error));
  
    // Cleaner way using promise chaining
    note
      .save()
      .then(savedNote => savedNote.toJSON())
      .then(savedAndFormattedNote => {
        response.json(savedAndFormattedNote);
      })
      .catch(error => next(error));
  });
  
  app.put('/api/notes/:id', (request, response, next) => {
    const body = request.body;
  
    const note = {
      content: body.content,
      important: body.important
    };
    Note.findByIdAndUpdate(request.params.id, note, { new: true })
      .then(updatedNote => {
        response.json(updatedNote);
      })
      .catch(error => next(error));
  });
  
  const unknownEndpoint = (request, response) => {
    response.status(404).send({ error: 'unknown endpoint' });
  };
  app.use(unknownEndpoint);
  
  const errorHandler = (error, request, response, next) => {
    console.log(error.message);
  
    if (error.name === 'CastError') {
      return response.status(400).send({ error: 'malformatted id' });
    } else if (error.name === 'ValidationError') {
      // Expand error handler to deal with validation errors
      return response.status(400).json({ error: error.message });
    }
  
    next(error);
  };
  
  app.use(errorHandler);
  
  const PORT = process.env.PORT || 3001;
  app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
  });