-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (82 loc) · 2.53 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
import express from 'express';
import morgan from 'morgan';
import { Low } from 'lowdb';
import { JSONFile } from 'lowdb/node';
const app = express();
app.use(morgan('dev'));
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
const PORT = process.env.PORT || 3000;
const adapter = new JSONFile('./database.json');
const db = new Low(adapter);
await db.read();
db.data ||= { pokemon: [] };
app.get('/', (req, res) => {
const page = `<h1>Hello Pokemon!</h1>`;
res.send(page);
});
app.get('/pokemon', (req, res) => {
res.json(db.data);
});
app.post('/pokemon', (req, res) => {
const { pokemonName, hitPoints, attack, defense, description } = req.body;
console.log({ pokemonName, hitPoints, attack, defense, description });
const newPokemon = {
name: pokemonName,
hitPoints,
attack,
defense,
description,
};
db.data.unshift(newPokemon);
db.write();
res.json({ result: 'success', data: newPokemon });
});
app.get('/pokemon/:id', (req, res) => {
const pathId = parseInt(req.params.id, 10);
const result = db.data.find((item) => pathId === item.id);
res.json(result);
});
app.get('/search', (req, res) => {
const query = req.query;
if (Object.keys(query).length === 0) {
res.json(db.data);
}
const queryKeys = Object.keys(req.query);
const result = db.data.filter((pokemon) => {
return queryKeys.every((key) => {
// Description keyword search
if (key === 'description') {
return pokemon[key].includes(req.query[key]);
}
// String comparisons
if (typeof pokemon[key] === 'string') {
return pokemon[key] === req.query[key];
}
// Number comparisons
if (typeof pokemon[key] === 'number') {
return pokemon[key] === parseInt(req.query[key], 10);
}
// Boolean comparisons
if (typeof pokemon[key] === 'boolean') {
return pokemon[key] === req.query[key];
}
// Array comparisons, i.e. Abilities
if (typeof pokemon[key] === 'object' && Array.isArray(pokemon[key])) {
console.log({ key: pokemon[key] });
return pokemon[key].includes(req.query[key]);
}
// Object comparisons, i.e. Evolution
// /search?evolution=name:venusaur
if (typeof pokemon[key] === 'object') {
const [queryKey, queryValue] = req.query[key].split(':');
return pokemon[key][queryKey] === queryValue;
}
});
});
res.json(result);
});
app.listen(PORT, () => {
console.log(`App listening on http://localhost:${PORT}/`);
});