-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (93 loc) · 2.9 KB
/
server.js
File metadata and controls
109 lines (93 loc) · 2.9 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
100
101
102
103
104
105
106
107
108
109
var express = require('express');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var cors = require('cors');
var app = express();
// This will be a proper database later
var fakeDB = [
{
"name": "Lorem Ipsum",
"bio" : "I am a teapot short and stout",
"git": "https://www.github.com/LoremIpsum"
},
{
"name": "Animi minus",
"bio": "btw Muirhead is the best building on UoB Campus",
"git": "https://www.github.com/Animagus"
}];
app.get('/', function (req, res) {
res.send('Hello World');
});
app.get('/api/0/profile/:id', function (req, res) {
// If the profile 'does not exist', error
console.log(req.params.id);
if(!(parseInt(req.params.id) >= 0 && parseInt(req.params.id) < fakeDB.length)){
res.status(404).send("profile not found");
res.end();
return;
}
// 'Query' the database, and respond with the profile
res.json(fakeDB[req.params.id]);
});
app.put('/api/0/profile/:id', jsonParser, function (req, res) {
// If the profile 'does not exist', error
console.log(req.params.id);
if(!(parseInt(req.params.id) >= 0 && parseInt(req.params.id) < fakeDB.length)){
res.status(404).send("profile not found");
res.end();
return;
}
// Modify the profile
console.log(req.body);
fakeDB[parseInt(req.params.id)] = req.body;
res.status(201).send("OK");
});
app.post('/api/0/profile/:id', jsonParser, function (req, res) {
// If authentication fails, error
if(req.body.auth != "3282c8027771f72b2def2520135daae5"){
res.status(403).send("authorisation failed");
return;
}
// If the profile exists, error
console.log(req.params.id);
if(parseInt(req.params.id) >= 0 && parseInt(req.params.id) < fakeDB.length){
res.status(403).send("duplicate profile id");
res.end();
return;
}
// Add the dummy object
let dummyObject = {
"name": "No name",
"bio": "No bio",
"git": "No git"
};
//Ignores the id parameter but lets be real this is a PoS PoC
fakeDB.push(dummyObject);
res.status(201).send("Created");
res.end();
return;
});
app.delete('/api/0/profile/:id', jsonParser, function (req, res) {
// If authentication fails, error
if(req.body.auth != "3282c8027771f72b2def2520135daae5"){
res.status(403).send("authorisation failed");
return;
}
// If the profile 'does not exist', error
console.log(req.params.id);
if(!(parseInt(req.params.id) >= 0 && parseInt(req.params.id) < fakeDB.length)){
res.status(404).send("profile not found");
res.end();
return;
}
// Delete the profile
fakeDB[req.params.id] = {};
res.status(200).send("OK");
res.end();
return;
});
var server = app.listen(8087, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})