-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cjs
105 lines (85 loc) · 2.31 KB
/
server.cjs
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
96
97
98
99
100
101
102
103
104
105
const express = require('express');
const path = require('path');
const fetch = require('node-fetch')
let port = process.env.PORT || 3000
console.log(port)
const serverless = require('serverless-http')
const cors = require("cors");
const { appendFile } = require('fs');
const server = express();
const router = express.Router()
server.use(express.static(path.join(__dirname, './dist/dog-search')));
server.use(cors())
server.get('/client', async (req, res) => {
res.sendFile(path.resolve(__dirname, './dist/dog-search', 'index.html'))
console.log('client section')
});
server.get('/api/:dogo', async (req, res) => {
const options = {
"method": "GET",
}
let dogoArr = req.params.dogo.split("")
for(let i = 0; i < dogoArr.length; i++) {
if(dogoArr[i] === "-") {
dogoArr[i] = '/'
console.log('yup')
}
}
let dogo = dogoArr.join("")
console.log(`https://dog.ceo/api/breed/${req.params.dogo}/images/random`)
//affenpinscher
const response = await fetch(`https://dog.ceo/api/breed/${dogo}/images/random`, options)
.then(res => res.json())
.catch(e => {
console.error({
"message": "woops!",
error: e,
})
})
//console.log('RESPONSE: ', response)
res.json(response)
})
server.get('/api/all/:dogo', async (req, res) => {
const options = {
"method": "GET",
}
let dogoArr = req.params.dogo.split("")
for(let i = 0; i < dogoArr.length; i++) {
if(dogoArr[i] === "-") {
dogoArr[i] = '/'
console.log('yup')
}
}
let dogo = dogoArr.join("")
console.log(`https://dog.ceo/api/breed/${req.params.dogo}/images`)
//affenpinscher
const response = await fetch(`https://dog.ceo/api/breed/${dogo}/images`, options)
.then(res => res.json())
.catch(e => {
console.error({
"message": "woops!",
error: e,
})
})
//console.log('RESPONSE: ', response)
res.json(response)
})
server.get('/api/breeds/', async (req, res) => {
const options = {
"method": "GET",
}
//affenpinscher
const response = await fetch(`https://dog.ceo/api/breeds/list/all`, options)
.then(res => res.json())
.catch(e => {
console.error({
"message": "woops!",
error: e,
})
})
//console.log('RESPONSE: ', response)
res.json(response)
})
module.exports.handler = serverless(server)
//server.use('/.netlify/functions/server', router)
server.listen(port, () => console.log('App Running on port 3000'));