-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (93 loc) · 2.91 KB
/
app.js
File metadata and controls
122 lines (93 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
const crypto = require('node:crypto')
const express = require('express')
const app = express()
const games = require('./games/games.json')
const { validateNewGame, validatePartialGame } = require('./validations/games')
const cors = require('cors')
const PORT = process.env.PORT ?? 53400
app.disable('x-powered-by')
// Middlewares
const allowedOrigins = ['http://localhost:8080'] // ['https://example.com', 'https://anotherdomain.com'];
const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.indexOf(origin !== -1)) {
callback(null, allowedOrigins)
} else {
callback(new Error('Error with my CORS'), null) // error, and options
}
}
}
app.use(cors(corsOptions))
app.use(express.json())
// API Methods
app.get('/games', (req, res) => {
return res.json({ games, totalCount: games.length })
})
app.get('/games/:id', (req, res) => {
const { id } = req.params
const gameIndex = games.findIndex((game) => game.id === id)
if (gameIndex === -1) {
return res.status(404).json({ message: 'Game not found' })
}
return res.json(games[gameIndex])
})
app.post('/games', (req, res) => {
const result = validateNewGame(req.body)
if (!result.success) {
return res.status(400).json({ message: result.error.errors })
}
const exist = games.some((game) => {
return game.name.toLowerCase() === result.data.name.toLowerCase()
})
if (exist) return res.status(500).json({ message: 'Game already exists' })
const newGame = {
id: crypto.randomUUID(),
...result.data
}
games.push(newGame)
return res.status(201).json(newGame)
})
app.put('/games/:id', (req, res) => {
const { id } = req.params
const gameIndex = games.findIndex((game) => game.id === id)
if (gameIndex === -1) {
return res.status(404).json({ message: 'Game not found' })
}
const result = validateNewGame(req.body)
if (!result.success) {
return res.status(400).json({ message: result.error.errors })
}
games[gameIndex] = {
...games[gameIndex],
...result.data
}
return res.json(games[gameIndex])
})
app.patch('/games/:id', (req, res) => {
const { id } = req.params
const gameIndex = games.findIndex((game) => game.id === id)
if (gameIndex === -1) {
return res.status(404).json({ message: 'Game not found' })
}
const result = validatePartialGame(req.body)
if (!result.success) {
return res.status(400).json({ message: result.error.errors })
}
games[gameIndex] = {
...games[gameIndex],
...result.data
}
return res.json(games[gameIndex])
})
app.delete('/games/:id', (req, res) => {
const { id } = req.params
const gameIndex = games.findIndex((game) => game.id === id)
if (gameIndex === -1) {
return res.status(404).json({ message: 'Game not found' })
}
games.splice(gameIndex, 1)
return res.json({ games, totalCount: games.length })
})
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT}`)
})