-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (70 loc) · 2.52 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
require('dotenv').config();
const axios = require('axios');
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.static('public'));
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
let deckID;
let drawCount = 1;
let playerCard;
//Deck of Cards API URL
const getNewCardsUrl = 'https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1';
//Requests a new deck and stores deck value for later draws
app.get('/getDeck', async (req, res) => {
try {
const response = await axios.get(getNewCardsUrl);
deckID = response.data.deck_id;
console.log(deckID)
res.json({ deck_id: deckID,});
return deckID;
}
catch(error) {
console.log(`Error getting new deck`, error);
res.status(500).json({error: 'Failed getting new deck'})
}
})
// Uses previously stored deck ID to shuffle
app.get('/shuffleDeck', async (req, res) => {
const shuffleUrl = `https://www.deckofcardsapi.com/api/deck/${deckID}/shuffle/`
try {
const response = await axios.get(shuffleUrl);
deckID = response.data.deck_id;
}
catch(error) {
console.log(`Error shuffling deck`, error);
res.status(500).json({error: 'Failed shuffling new deck'})
}
})
//Uses deck value to call for a new card
app.get('/drawCard', async(req, res) => {
const drawUrl = `https://www.deckofcardsapi.com/api/deck/${deckID}/draw/?count=1`
try {
const response = await axios.get(drawUrl);
playerCard = response.data.cards[0].code; // temp stores card for later calls
cardImage = response.data.cards[0].image;
const remaining= response.data.remaining;
res.json({card: playerCard, img: cardImage, remaining: remaining })
}
catch(error) {
console.log('Error drawing card');
res.status(500).json({error: 'Failed drawing card'})
}
})
//Giphy API for searching gifs
app.get('/getGif', async (req, res) => {
const gifSearch = req.query.search;
const gifAPI = process.env.GIF_API_KEY;
let gifRating = ['g', 'pg', 'pg-13', 'r'];
let gifUrl = `https://api.giphy.com/v1/gifs/random?api_key=${gifAPI}&tag=${gifSearch}&rating=${gifRating[3]}`;
try {
const response = await axios.get(gifUrl);
const directGifUrl = response.data.data.bitly_gif_url;
res.json({url: response.data.data.images.original.url});
}
catch(error) {
res.status(500).json({error: 'Failed to retrieve GIF'})
}
})