-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (53 loc) · 2.14 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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
const API_URL = "https://api.exchangerate-api.com/v4/latest/";
app.set('view engine', 'ejs'); // Use EJS for rendering HTML
app.use(bodyParser.urlencoded({ extended: true }));
// Function to fetch exchange rates
async function fetchExchangeRate(baseCurrency) {
const response = await axios.get(`${API_URL}${baseCurrency}`);
return response.data.rates;
}
// Function to calculate basket value
async function calculateBasketValue(baseCurrency, basket, weights) {
const rates = await fetchExchangeRate(baseCurrency);
let basketValue = 0;
for (let i = 0; i < basket.length; i++) {
if (rates[basket[i]]) {
basketValue += rates[basket[i]] * weights[i];
}
}
return basketValue;
}
// Define the index route
app.get('/', (req, res) => {
res.render('index', { basket_value: null, base_currency: null, historical_values: [], dates: [] });
});
app.post('/', async (req, res) => {
const baseCurrency = req.body.base_currency;
const usdWeight = parseFloat(req.body.usd_weight);
const eurWeight = parseFloat(req.body.eur_weight);
const jpyWeight = parseFloat(req.body.jpy_weight);
// Example basket and weights
const basket = ['USD', 'EUR', 'JPY'];
const weights = [usdWeight, eurWeight, jpyWeight];
// Calculate basket value
const basketValue = await calculateBasketValue(baseCurrency, basket, weights);
// Fake data for historical values and dates
const historicalValues = Array.from({ length: 5 }, (_, i) => basketValue * (i + 1));
const dates = ['2022-01', '2022-02', '2022-03', '2022-04', '2022-05'];
// Return to the template with calculated values and historical data
res.render('index', {
basket_value: basketValue,
base_currency: baseCurrency,
historical_values: historicalValues,
dates: dates
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});