-
Notifications
You must be signed in to change notification settings - Fork 607
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (87 loc) · 2.26 KB
/
Copy pathindex.js
File metadata and controls
99 lines (87 loc) · 2.26 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express');
const Hubs = require('./hubs/hubs-model.js');
const server = express();
server.use(express.json());
server.get('/', (req, res) => {
res.send(`
<h2>Lambda Hubs API</h>
<p>Welcome to the Lambda Hubs API</p>
`);
});
server.get('/api/hubs', async (req, res) => {
try {
const hubs = await Hubs.find(req.query);
res.status(200).json(hubs);
} catch (error) {
// log error to database
console.log(error);
res.status(500).json({
message: 'Error retrieving the hubs',
});
}
});
server.get('/api/hubs/:id', async (req, res) => {
try {
const hub = await Hubs.findById(req.params.id);
if (hub) {
res.status(200).json(hub);
} else {
res.status(404).json({ message: 'Hub not found' });
}
} catch (error) {
// log error to database
console.log(error);
res.status(500).json({
message: 'Error retrieving the hub',
});
}
});
server.post('/api/hubs', async (req, res) => {
try {
const hub = await Hubs.add(req.body);
res.status(201).json(hub);
} catch (error) {
// log error to database
console.log(error);
res.status(500).json({
message: 'Error adding the hub',
});
}
});
server.delete('/api/hubs/:id', async (req, res) => {
try {
const count = await Hubs.remove(req.params.id);
if (count > 0) {
res.status(200).json({ message: 'The hub has been nuked' });
} else {
res.status(404).json({ message: 'The hub could not be found' });
}
} catch (error) {
// log error to database
console.log(error);
res.status(500).json({
message: 'Error removing the hub',
});
}
});
server.put('/api/hubs/:id', async (req, res) => {
try {
const hub = await Hubs.update(req.params.id, req.body);
if (hub) {
res.status(200).json(hub);
} else {
res.status(404).json({ message: 'The hub could not be found' });
}
} catch (error) {
// log error to database
console.log(error);
res.status(500).json({
message: 'Error updating the hub',
});
}
});
// add an endpoint that returns all the messages for a hub
// add an endpoint for adding new message to a hub
server.listen(4000, () => {
console.log('\n*** Server Running on http://localhost:4000 ***\n');
});