-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (60 loc) · 2.69 KB
/
index.js
File metadata and controls
68 lines (60 loc) · 2.69 KB
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
import express from "express";
import axios from "axios";
const app = express();
const port = 5000;
const baseURL = "https://blockchain.info";
app.use(express.static("public")); // declares the public folder as static
app.use(express.urlencoded({ extended: true })); // express middleware - important for processing form data
var data = {
allRates: undefined,
userRates: ['USD', 'CAD', 'EUR', 'GBP', 'JPY'],
conversionBTC: undefined,
conversionGeneral: undefined
}
/*Fetching data */
app.get("/", async (req, res) => {
try {
const exchangeRates = await axios.get(baseURL + "/ticker");
data.allRates = exchangeRates.data;
res.render("index.ejs", data);
} catch (error) {
res.send("500 Internal Server Error. Something went wrong on our side.");
console.error(error.message);
}
});
app.get("/exchange-rates", (req, res) => {
res.render("exchange-rates.ejs", data);
});
app.post("/btc-conversion", async (req, res) => {
try {
const exchangeRates = await axios.get(baseURL + "/ticker");
data.rate = exchangeRates.data;
const result = await axios.get(baseURL + "/tobtc?currency=" + req.body.currency + "&value=" + req.body.amount);
data.conversionBTC = req.body.amount + " " + req.body.currency + " = " + result.data.toFixed(3) + " BTC";
res.render("index.ejs", data);
} catch (error) {
res.send("500 Internal Server Error. Something went wrong on our side.");
console.error(error.message);
}
});
app.post("/general-conversion", async (req, res) => {
/* Convert both amounts to BTC, then find the ratio
Technically not super accurate (especially because exchange rates might update between
the two get requests), but it's a very good approximation */
try {
// Debugged an error 500 here, it was because of a variable name that I spelt wrong
const exchangeRates = await axios.get(baseURL + "/ticker");
data.rate = exchangeRates.data;
const first = await axios.get(baseURL + "/tobtc?currency=" + req.body.currency1 + "&value=" + req.body.amount);
const second = await axios.get(baseURL + "/tobtc?currency=" + req.body.currency2 + "&value=1");
const ans = first.data / second.data;
data.generalConversion = req.body.amount + " " + req.body.currency1 + " = " + ans.toFixed(3) + " " + req.body.currency2; // somehow JS figures out that these are actually integers, not strings
res.render("index.ejs", data);
} catch (error) {
res.send("500 Internal Server Error. Something went wrong on our side.");
console.error(error.message);
}
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});