-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (71 loc) · 2.03 KB
/
index.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
const express = require('express')
const cors = require('cors')
const LoremIpsum = require("lorem-ipsum").LoremIpsum
const heros = require('./data/heros.json')
const lorem = new LoremIpsum({
sentencesPerParagraph: { max: 8, min: 4 },
wordsPerSentence: { max: 16, min: 4 },
})
const randomIntFromInterval = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
const PORT = 4000
const app = express()
app.use(cors())
app.get('/', (req, res) => {
res.send(`server is running on port: ${PORT}`)
})
app.get('/hero', (req, res) => {
const heroList = heros.map((hero) => ({
id: `${hero.id}`,
name: hero.localized_name,
attr: hero.primary_attr,
attackType: hero.attack_type,
img: hero.img,
icon: hero.icon,
health: hero.base_health,
mana: hero.base_mana,
armor: hero.base_armor,
minAttack: hero.base_attack_min,
maxAttack: hero.base_attack_max,
str: hero.base_str,
agi: hero.base_agi,
int: hero.base_int,
desc: lorem.generateParagraphs(randomIntFromInterval(5, 8)),
}))
res.json({ data: heroList })
})
app.get('/hero/:id', (req, res) => {
try {
const idIn = req.params.id
const hero = heros.find(({ id }) => `${id}` === `${idIn}`)
if (hero) {
res.json({
data: {
id: `${hero.id}`,
name: hero.localized_name,
attr: hero.primary_attr,
attackType: hero.attack_type,
img: hero.img,
icon: hero.icon,
health: hero.base_health,
mana: hero.base_mana,
armor: hero.base_armor,
minAttack: hero.base_attack_min,
maxAttack: hero.base_attack_max,
str: hero.base_str,
agi: hero.base_agi,
int: hero.base_int,
desc: lorem.generateParagraphs(randomIntFromInterval(5, 8)),
},
})
} else [
res.status(400).json({ error: `not found hero id: ${idIn}` })
]
} catch (e) {
res.status(400).json({ error: e })
}
})
app.listen(PORT, () => {
console.log('server is running on port:', PORT)
})