Friday, May 28, 2021

get method to retrieve a entry from api

 

const express = require("express");
const app = express();

const courses = [
  { id: 1name: "uday1" },
  { id: 2name: "uday2" },
  { id: 3name: "uday3" },
  { id: 4name: "uday4" },
];

app.get("/api/courses/:id", (reqres=> {
  const course = courses.find((c=> c.id === parseInt(req.params.id));
  if (!courseres.status(404).send("The course ID not found");
  res.send(course);
});

app.listen(3000, () => console.log("listening"));
const port = process.env.PORT || 3000;

//http://localhost:3000/api/courses/11

No comments:

Post a Comment