-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
52 lines (38 loc) · 1.3 KB
/
api.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
const express = require('express');
const axios = require('axios');
const fs = require('fs')
const router = express.Router();
const pictureCount = 4;
router.get('/get_pictures', (req, res) => {
currentPictures = []
const pictureRequests = []
category = req.query.category
let apiUrl = `https://foodish-api.herokuapp.com/api/images/${category}`
for (let i = 0; i < pictureCount; i++) {
pictureRequests.push(axios(apiUrl))
}
Promise.all(pictureRequests).then(responses => {
const pictures = []
for (const response of responses) {
pictures.push(response.data.image)
}
res.json(pictures)
}).catch(() => {
res.json([])
})
})
router.post('/save_pictures', (req, res) => {
const users = JSON.parse(fs.readFileSync('./users.json', 'utf8'))
const user = users.find(user => user.sid = req.user.sid)
const pictureUrl = req.body.pictureUrl
user.savedPictures.push(pictureUrl)
console.log("save picture")
fs.writeFileSync('./users.json', JSON.stringify(users))
res.end()
})
router.get('/saved_pictures', (req, res) => {
const users = JSON.parse(fs.readFileSync('./users.json', 'utf8'))
const user = users.find(user => user.sid = req.user.sid)
res.json(user.savedPictures)
})
module.exports = router