-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (54 loc) · 1.48 KB
/
server.js
File metadata and controls
58 lines (54 loc) · 1.48 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
const http = require('http');
const {
getAllData,
getData,
createData,
updateData,
deleteData,
} = require('./controllers/realmController');
const hostname = '127.0.0.1';
const PORT = process.env.PORT || 5000;
// Routes
const server = http.createServer((req, res) => {
if (
req.url === '/api/realms' ||
(req.url === '/api/realms/' && req.method === 'GET')
) {
getAllData(req, res);
} else if (
req.url.match(
/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
) &&
req.method === 'GET'
) {
const id = req.url.split('/')[3];
getData(req, res, id);
} else if (req.url === '/api/realms' && req.method === 'POST') {
createData(req, res);
} else if (
req.url.match(
/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
) &&
req.method === 'PUT'
) {
const id = req.url.split('/')[3];
updateData(req, res, id);
} else if (
req.url.match(
/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
) &&
req.method === 'DELETE'
) {
const id = req.url.split('/')[3];
deleteData(req, res, id);
} else {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify({ message: 'Route Not Found' }));
res.end();
}
});
server.listen(PORT, hostname, () => {
console.log(`Server running at http://${hostname}:${PORT}/`);
});
// JSON data from https://skjalden.com/nine-realms-in-norse-mythology/