-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (37 loc) · 1.03 KB
/
index.js
File metadata and controls
49 lines (37 loc) · 1.03 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
const express = require('express');
const { getData } = require('./utils.js');
const cors = require('cors');
const app = express();
const globalData = getData();
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send('hello world');
});
app.get('/all', (req, res) => {
console.log('request /all');
return res.status(200).send(getData());
});
app.get('/get/:id', (req, res) => {
const { id } = req.params;
console.log(`request to /get/${id}`);
for (let val of globalData) {
if (val.id === Number(id)) return res.status(200).send(val);
}
return res.status(404).send({});
});
app.get('/category', (req, res) => {
const { type } = req.body;
console.log(`request to /category with body ${req.body}`);
let catData = globalData.filter(val => {
return val.category === type;
});
if (catData.length) {
return res.status(200).send(catData);
}
return res.status(404).send([]);
});
app.listen(port, () => {
console.log(`server is up on port http://localhost:${port}`);
});