-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (109 loc) · 3.22 KB
/
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
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
106
107
108
109
110
111
112
113
114
115
116
117
const config = require('./config.js')
const express = require('express')
const bodyParser = require('body-parser');
const morgan = require('morgan')
const axios = require('axios').default
const app = express();
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// modifications to the response's http header
app.use(function (request, response, next) {
response.header('Access-Control-Allow-Origin', "*")
response.removeHeader('X-Powered-By')
response.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept, Authorization")
response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
next()
})
app.get('/information', async (req, res, next) => {
let arrAux = [];
let arrNational = [
{
name: 'PTR',
price: `${config.PTR} $`
},
{
name: 'BSF',
price: `${config.BS} $`
}
]
try {
for (const i in config.ARR_TYPE) {
let objAux;
try {
let coins = (await axios.get(config.ALTERNATIVE_URL + config.ARR_TYPE[i])).data;
objAux = {
name: config.ARR_TYPE[i],
price: `${coins[config.ARR_TYPE[i]].quotes.USD.price.toFixed(2)} $`
}
} catch (error) {
throw error;
}
arrAux.push(objAux)
};
const arr = Array().concat(arrNational, arrAux)
res.status(200).json({
status: 'Ok',
documents: arr
})
} catch (error) {
res.status(400).json({
status: 'Fail',
document: []
})
}
})
app.post('/calculate/prices', async (req, res, next) => {
let arrAux = [];
let coins = (await axios.get(config.ALTERNATIVE_URL + req.body.coin)).data;
let base = {
amount: req.body.amount,
coin: req.body.coin,
price: coins[req.body.coin].quotes.USD.price
};
let arrNational = [
{
name: 'PTR',
price: `${base.price / config.PTR } PTR`
},
{
name: 'BSF',
price: `${ base.price / config.BS_VALUE} BSF`
}
]
let arrType = config.ARR_TYPE.filter(function (x) {
if (x != base.coin) {
return x
}
});
try {
for (const i in arrType) {
let objAux;
try {
let coins = (await axios.get(config.ALTERNATIVE_URL + arrType[i])).data;
let price = base.price / coins[arrType[i]].quotes.USD.price;
objAux = {
name: arrType[i],
price: `${price.toFixed(2)} ${base.coin}`
}
} catch (error) {
throw error;
}
arrAux.push(objAux)
};
const arr = Array().concat(arrNational, arrAux)
base.result = arr;
res.status(200).json({
status: 'Ok',
documents: base
})
} catch (error) {
res.status(400).json({
status: 'Fail',
document: []
})
}
})
app.listen(3000, () => {
console.log('The server is started')
})