-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (42 loc) · 1.33 KB
/
app.js
File metadata and controls
48 lines (42 loc) · 1.33 KB
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
41
42
43
44
45
46
47
48
import express from 'express';
const app = express();
app.use(express.json());
const resources = [];
app.post('/resources', (request, response) =>
{
const newResource = request.body;
resources.push(newResource);
response.status(201).json(newResource);
});
app.get('/resources', (request, response) =>
{
response.status(200).json(resources);
});
app.put('/resources/:id', (request, response) =>
{
const { id } = request.params;
const updatedResource = request.body;
const resourceIndex = resources.findIndex((el) => el.id === parseInt(id));
if (resourceIndex === -1) {
response.status(404).send("Resource not found!");
return;
}
resources[resourceIndex] = { ...resources[resourceIndex], ...updatedResource };
response.status(200).send("Resource Updated Successfully");
});
app.delete('/resources/:id', (request, response) =>
{
const { id } = request.params;
const resourceIndex = resources.findIndex(el => el.id === id);
if (resourceIndex == -1)
{
response.status(404).send("Resource not found!");
return;
}
resources.splice(resourceIndex, 1);
response.status(200).send("Resource deleted successfully");
});
app.listen(3000, () =>
{
console.log(`Server is running on http://localhost:3000`);
});