-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (111 loc) · 2.91 KB
/
server.js
File metadata and controls
122 lines (111 loc) · 2.91 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
110
111
112
113
114
115
116
117
118
119
120
121
122
/** A simple REST server built with express */
const shortid = require('shortid');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
/* Setup express middlewares */
app.use(bodyParser.json());
app.use(allowCorsMiddleware);
/* API */
app.get('/game', getGames);
app.get('/game/:id', getGame);
app.put('/game/:id', editGame);
app.post('/game', addGame);
app.delete('/game/:id', deleteGame);
/* Health endpoint */
app.get('/health', getHealth);
/* Start server */
app.listen(3000, () =>
console.log(
`app listening on port ${PORT}. Go to http://localhost:${PORT}/health to test that the server is running.`
)
);
/* The mock 'database' */
let games = [
{ id: '23TplPdS', name: 'Dwarf Fortress' },
{ id: '46Juzcyx', name: 'The Sims 2' },
{ id: '2WEKaVNO', name: 'Elasto Mania' },
{ id: 'nYrnfYEv', name: 'Team Fortress 2' },
];
/* IMPLEMENTATION DETAILS */
/* Return a list of all gmes
* Example: localhost:3000/game
*/
function getGames(req, res) {
return res
.status(200)
.json(games)
.end();
}
/* Return a specific game based on id
* Example: localhost:3000/game/23TplPdS
*/
function getGame(req, res) {
const id = req.params.id;
const game = games.find(g => g.id == id);
return game
? res
.status(200)
.json(game)
.end()
: res.status(404).end();
}
/* Add a new game to the list
* Example: localhost:3000/game
* Body: { "name": "Fresh Prince" } */
function addGame(req, res) {
const name = req.body.name;
if (!name) {
return res.status(401).end();
}
const newGame = { id: shortid.generate(), name };
games = [...games, newGame];
return res
.status(201)
.json(newGame)
.end();
}
/* Delete a game from the list
* Example: localhost:3000/game/23TplPdS
*/
function deleteGame(req, res) {
const id = req.params.id;
const removedGame = games.find(g => g.id == id);
games = games.filter(g => g.id != id);
return res
.status(200)
.json(removedGame)
.end();
}
/* Edit an existing game in the list
* Example: localhost:3000/game/23TplPdS
* Body: { "name": "Minesweeper" }
*/
function editGame(req, res) {
const id = req.params.id;
const name = req.body.name;
if (!name || !id) {
return res.status(400).end();
}
games = games.map(g => (g.id == id ? { ...g, name } : g));
return res
.status(200)
.json(games.find(g => g.id == id))
.end();
}
function getHealth(req, res) {
const msg = `The server is running. Try http://localhost:${PORT}/game to list all games.`;
return res
.status(200)
.send(msg)
.end();
}
/* MISC */
/* Add CORS-headers to every request */
function allowCorsMiddleware(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
}