-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (41 loc) · 1.15 KB
/
app.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
41
42
43
44
45
46
47
48
49
50
51
const express = require('express');
const app = express();
const port = 3000;
let items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
];
app.get('/items', (req, res) => {
res.json(items);
});
app.get('/items/:id', (req, res) => {
const itemId = parseInt(req.params.id);
const item = items.find(item => item.id === itemId);
if (item) {
res.json(item);
} else {
res.status(404).json({ message: 'Item not found' });
}
});
app.get('/message', (req, res) => {
res.send('Hello from the server!');
});
app.put('/items/:id', express.json(), (req, res) => {
const itemId = parseInt(req.params.id);
const updatedItem = req.body;
const index = items.findIndex(item => item.id === itemId);
if (index !== -1) {
items[index] = { ...items[index], ...updatedItem };
res.json(items[index]);
} else {
res.status(404).json({ message: 'Item not found' });
}
});
app.put('/message', express.json(), (req, res) => {
const updatedMessage = req.body.message;
res.send(`Updated message: ${updatedMessage}`);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});