eolas/Programming_Languages/NodeJS/REST_APIs/4_DELETE.md

20 lines
421 B
Markdown
Raw Normal View History

2022-08-04 15:00:04 +01:00
---
2022-08-28 10:37:22 +01:00
categories:
- Programming Languages
tags: [backend, node-js, REST, APIs]
2022-08-04 15:00:04 +01:00
---
2022-08-30 10:00:04 +01:00
# Creating a RESTful API: `DELETE` requests
2022-08-04 15:00:04 +01:00
```js
2022-08-30 10:00:04 +01:00
router.delete("/:id", (req, res) => {
2022-08-04 15:00:04 +01:00
const course = courses.find((c) => c.id === parseInt(req.params.id));
2022-08-30 10:00:04 +01:00
if (!course)
return res.status(404).send("A course with the given ID was not found");
2022-08-04 15:00:04 +01:00
courses.indexOf(course);
courses.splice(index, 1);
res.send(course);
});
2022-08-28 10:37:22 +01:00
```