In the HTTP Delete Requests everything is working except for when I use my GET method on Post Man. I’m using http://localhost:3000/api/courses address but after I click Send, course 1 is gone as expected, but so is course 3…
Why is this happening?
Here’s my index.js:
const Joi = require("joi"); // Validation code
const express = require("express");
const app = express();
// Enable json objects from post
app.use(express.json());
const courses = [
       { id: 1, name: "courses1" },
       { id: 2, name: "courses2" },
       { id: 3, name: "courses3" },
  ];
 // Respond to end points
 // Obtain paths using get
app.get("/", (req, res) => {
      res.send("Hello World");
});
app.get("/api/courses", (req, res) => {
    res.send(courses);
});
// Post json data
// Joi version 17
app.post("/api/courses", (req, res) => {
// Validate course update
       const { error } = validateCourse(req.body); // Result.error
  if (error) {
 // Only reveal message, otherwise show all details of error
    res.status(400).send(result.error.details[0].message);
   }
   const course = {
      id: courses.length + 1,
     name: req.body.name,
   };
   // Append new elements
    courses.push(course);
   // Send a response
      res.send(course);
 });
  // Update HTTP Requests
    app.put("/api/courses/:id", (req, res) => {
   // params = parameters ex. year and month
  // query: optional paramters
  //res.send(req.params);
   const course = courses.find((c) => c.id === parseInt(req.params.id));
        if (!course)
         res.status(404).send("The course with the given ID was not found.");
    // Validate course update
       const { error } = validateCourse(req.body); // Result.error
         if (error) {
     // Only reveal message, otherwise show all details of error
        res.status(400).send(error.details[0].message);
          }
    // Update course
        course.name = req.body.name;
  // Return the updated course
         res.send(course);
      });
  // Delete Request
        app.delete("/api/courses/:id", (req, res) => {
       const course = courses.find((c) => c.id === parseInt(req.params.id));
       if (!course)
       res.status(404).send("The course with the given ID was not found.");
   // Delete
        const index = courses.indexOf(course);
    // Go to the index and remove 1 object
    // splice(start: number, deleteCount?: number)
         courses.splice(index, 1);
        res.send(course);
      });
        // Joi version 17
          function validateCourse(course) {
             const schema = Joi.object({
           name: Joi.string().min(3).required(),
         });
        return schema.validate(course);
        }
        // /api/courses/1
      app.get("/api/courses/:id", (req, res) => {
      // params = parameters ex. year and month
     // query: optional paramters
       //res.send(req.params);
          const course = courses.find((c) => c.id === parseInt(req.params.id));
           if (!course)
                 res.status(404).send("The course with the given ID was not found."); // 404
                res.send(course);
       });
    // PORT
   // env: environment variables
  // PORT: environment varoable
   // Use 'set' to create port for environment variable or use 3000
   // set PORT=?
     const port = process.env.PORT || 3000;
   // Apply port in console.log using template literals (strings combined with 
   implanted expressions)
     app.listen(port, () => console.log(`Listening on port ${port}...`));