-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (48 loc) · 1.76 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
const express = require('express')
const axios = require('axios')
const clc = require("cli-color")
const http = require('http')
const https = require('https')
const fs = require('fs');
var ExpressBrute = require('express-brute');
var store = new ExpressBrute.MemoryStore(); // stores state locally, don't use this in production
var bruteforce = new ExpressBrute(store);
require('dotenv').config()
// Defining app
const app = express()
const port = process.env.APP_PORT;
const sslport = process.env.APP_SSL_PORT;
// SSL Config
if(process.env.SSL == 1) {
options = {
cert: fs.readFileSync(process.env.SSL_DIR + 'fullchain.pem'),
key: fs.readFileSync(process.env.SSL_DIR + 'privkey.pem')
}
}
app.get('/:amount/:from/:to', bruteforce.prevent, (req, res) => {
data = req.params
const httpsAgent = new https.Agent({
rejectUnauthorized: process.env.DEVELOPER ? false : true,
})
axios.get(process.env.API_URL + data.amount + '/' + data.from + '/' + data.to, { httpsAgent })
.then(function (response) {
// Making a beauty currency amount response
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: response.data.to_short,
});
res.send(`${clc.red(response.data.amount)} ${clc.yellow(response.data.from_name)} = ${clc.green(formatter.format(response.data.result))}`)
})
.catch(function (error) {
console.log(error)
})
})
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(options, app);
httpServer.listen(port, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(sslport, () => {
console.log('HTTPS Server running on port 443');
});