-
Notifications
You must be signed in to change notification settings - Fork 0
/
38deleterequest.js
41 lines (33 loc) · 1010 Bytes
/
38deleterequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const express = require('express');
const fs = require('fs');
const port = 3000;
const app = express();
const movies = JSON.parse(fs.readFileSync('./data/movies.json'));
app.use(express.json());
app.delete('/api/v1/movies/:id', (req, res) => {
const id = parseInt(req.params.id);
const movieToDelete = movies.find(el => el.id === id);
if (!movieToDelete) {
return res.status(404).json({
status: 'Failure',
message: "The movie is not available with ID " + id,
});
}
const index = movies.indexOf(movieToDelete);
movies.splice(index, 1);
fs.writeFile('./data/movies.json', JSON.stringify(movies), (err) => {
if (err) {
return res.status(500).json({
status: 'error',
message: 'Failed to delete movie',
});
}
res.status(204).json({
status: 'success',
message: "The movie with ID " + id + " has been deleted successfully",
});
});
});
app.listen(port, () => {
console.log('Server is running on port ' + port);
});