-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
192 lines (176 loc) · 4.91 KB
/
server.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// server.js
// where your node app starts
// init project
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// Global letiables to represent Database
let TODOS = {
'todo1': { 'task': 'build an API'},
'todo2': { 'task': '?????'},
'todo3': { 'task': 'profit!'},
};
let HEROES = [
{'id': 1, 'name': 'Batman', 'age': 55},
{'id': 2, 'name': 'Wonder Woman', 'age': 25},
{'id': 3, 'name': 'Damntrecky', 'age': 30},
];
/**
* @description Get the hero object based on id
* @param {*} id
*/
function getHeroByIndex(id) {
for (let i=1;i<Object.keys(HEROES).length;i++) {
if (HEROES[i].id == id) return i;
}
return -1;
}
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/zinggrid.html');
});
// http://expressjs.com/en/starter/basic-routing.html'
// get list of todos
app.get('/todos', (req, res) => {
try {
res.status(200).send(TODOS);
} catch(e) {
res.status(500).send('Error fetching /todos');
}
});
// create todo
app.post('/todos', (req, res) => {
try {
let newTodo = req.body;
let todoID = `todo${Object.keys(TODOS).length + 1}`;
TODOS[todoID] = newTodo
res.status(200).send(todoID);
} catch(e) {
res.status(500).send('Error creating /todos');
}
});
// get a single todo
app.get('/todos/:todoId', (req, res) => {
try {
let todoID = req.params.todoId;
if (!TODO[todoID]) res.status(404).send(`Todo {} doesn't exist ${todoID}`);
else res.status(200).send(TODO[todoID]);
} catch(e) {
res.status(500).send(`Error getting /todos/${todoID}`);
}
});
// delete a hero
app.delete('/todos/:todoId', (req, res) => {
try {
let todoID = req.params.todoId;
if (!TODOS[todoID]) res.status(404).send(`Todo {} doesn't exist ${todoID}`);
else {
delete TODOS[todoID];
res.sendStatus(204);
}
} catch(e) {
res.status(500).send(`Error getting /todos/${todoID}`);
}
});
// function to update hero with PATCH/PUT
function updateTodo(req, res) {
try {
let todoID = req.params.todoId;
let updateBody = req.body;
if (!TODOS[todoID]) res.status(404).send(`Todo {} doesn't exist ${todoID}`);
else {
let todoObj = TODOS[todoID];
// update all keys but id
for (key in updateBody) {
todoObj[key] = updateBody[key];
}
res.status(201).send(todoObj);
}
} catch(e) {
res.status(500).send(`Error updating /todos/${todoID}`);
}
};
// update a hero field (single cell update)
app.patch('/todos/:todoId', updateTodo);
app.put('/todos/:todoId', updateTodo);
// get a list of heroes
app.get('/heroes', (req, res) => {
try {
res.status(200).send(HEROES);
} catch(e) {
res.status(500).send('Error fetching /heroes');
}
});
// create a hero
app.post('/heroes', (req, res) => {
try {
let newHero = req.body;
let heroID = Object.keys(HEROES).length;
newHero.id = heroID;
HEROES.push(newHero);
res.status(201).send(newHero);
} catch(e) {
res.status(500).send('Error creating /todos');
}
});
// get a single hero
app.get('/heroes/:id', (req, res) => {
try {
let heroID = req.params.id;
let heroIndex = getHeroByIndex(heroID);
if (heroIndex === -1) res.status(404).send(`Hero {} doesn't exist ${heroID}`);
else {
res.status(200).send(HEROES[heroIndex]);
}
} catch(e) {
res.status(500).send(`Error getting /heroes/${heroID}`);
}
});
// delete a hero
app.delete('/heroes/:id', (req, res) => {
try {
let heroID = req.params.id;
let heroIndex = getHeroByIndex(heroID);
if (heroIndex === -1) res.status(404).send(`Hero {} doesn't exist ${heroID}`);
else {
delete HEROES[heroIndex];
res.sendStatus(204);
}
} catch(e) {
res.status(500).send(`Error getting /heroes/${heroID}`);
}
});
// function to update hero with PATCH/PUT
function updateHero(req, res) {
try {
let heroID = req.params.id;
let updateBody = req.body;
let heroIndex = getHeroByIndex(heroID);
if (heroIndex === -1) res.status(404).send(`Hero {} doesn't exist ${heroID}`);
else {
let heroObj = HEROES[heroIndex];
// update all keys but id
for (let key in updateBody) {
if (key != 'id') heroObj[key] = updateBody[key];
}
res.status(201).send(heroObj);
}
} catch(e) {
res.status(500).send(`Error updating /heroes/${heroID}`);
}
};
// update a hero field (single cell update)
app.patch('/heroes/:id', updateHero);
app.put('/heroes/:id', updateHero);
// listen for requests :)
const listener = app.listen(process.env.PORT || 8000, () => {
console.log('Your app is listening on port ' + listener.address().port);
});