eolas/Programming_Languages/NodeJS/REST_APIs/3_PUT.md
2022-08-28 10:37:22 +01:00

1 KiB

categories tags
Programming Languages
backend
node-js
REST
APIs

RESTful API with Node, Express and MongoDB: PUT requests

To demonstrate the handling of PUT requests, we will create a handler that updates an element in the course array, based on its id and return the updated entry:

router.put('/:id', (req, res) => {
  const course = courses.find((c) => c.id === parseInt(req.params.id));

  if (!course) return res.status(404).send('A course with the given ID was not found');
  const {error} = validateCourse(req.body);

  if (error) return error.details.map((joiErr) => res.status(400).send(joiErr.message));

  course.name = req.body.name;
  res.send(course);
});

Our request:

const updateCourse = async (courseChange) => {
  try {
    const resp = await axios.put('http://localhost:3000/api/courses/1', {
      name: courseChange.name,
    });
    console.log(resp.data);
  } catch (err) {
    console.error(err);
  }
};

updateCourse({name: 'A new course'});

Returns:

{ id: 1, name: 'A new course' }