-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratingsAPI.js
47 lines (35 loc) · 1.15 KB
/
ratingsAPI.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
/* eslint no-console: 0 */
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const buffer = fs.readFileSync('./data.json');
const allObj = JSON.parse(buffer);
const app = express();
app.use(cors());
const catego = allObj.categories;
const posto =allObj.posts;
app.get('/posts/:id', (req, res) => {
const post = posto.find(item => item.id === req.params.id);
console.log(post);
if (post) {
console.log(post.author);
setTimeout(() => res.json(post), Math.floor(Math.random() * 5000));
} else {
console.log(404, req.params.id);
res.status(404).json({ error: 'show not found' });
}
});
app.get('/categories/:id', (req, res) => {
const category = catego.find(item => item.id === req.params.id);
console.log(category);
if (category) {
console.log(category.name);
setTimeout(() => res.json(category), Math.floor(Math.random() * 5000));
} else {
console.log(404, req.params.id);
res.status(404).json({ error: 'show not found' });
}
});
console.log(`Starting server on port 3000`);
console.log(`Generating new random ratings`);
app.listen(3000);