forked from juanmaguitar/proxy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (33 loc) · 994 Bytes
/
app.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
const request = require('request')
const cors = require('cors')
const express = require('express')
require('dotenv').load()
const app = express()
const { PORT, URL_BASE, TOKEN } = process.env
const options = {
headers: {
'User-Agent': 'request'
}
}
if (TOKEN) {
options.headers.Authorization = `Bearer ${TOKEN}`
}
let counter = 0
app.use(cors())
app.get('*', function (req, res) {
const urlRequested = req.originalUrl
console.log('-'.repeat(20))
console.log(`→ Attempt ${++counter}`)
console.log('path requested = ' + urlRequested)
if (urlRequested.includes('favicon.ico')) {
res.status(500).send("Couldn't get a JSON!")
}
options.url = URL_BASE + urlRequested
console.log('🔥 requesting: ' + options.url)
request(options, function (error, response, body) {
if (error) res.status(500).send('Something went wrong!')
const json = JSON.parse(body)
res.json(json)
})
})
app.listen(PORT, () => console.log(`listening on port ${PORT}...`))