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-28 10:37:22 +01:00
|
|
|
# RESTful API with Node, Express and MongoDB: `DELETE` requests
|
2022-08-04 15:00:04 +01:00
|
|
|
|
|
|
|
```js
|
2022-08-28 10:37:22 +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-28 10:37:22 +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
|
|
|
```
|