-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.54 KB
/
index.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
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = 3000;
// Middleware to parse URL-encoded bodies (form data)
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', {
prices: null,
equivalentAmountsUSD: null,
equivalentAmountsINR: null,
amount: null,
error: null
});
});
app.post('/convert', async (req, res) => {
const amountInUSD = req.body.amount;
try {
// Fetch cryptocurrency prices and USD to INR conversion rate
const [cryptoResponse, conversionResponse] = await Promise.all([
axios.get('https://api.coingecko.com/api/v3/simple/price', {
params: {
ids: 'bitcoin,ethereum,solana,dogecoin,binancecoin',
vs_currencies: 'usd'
}
}),
axios.get('https://api.coingecko.com/api/v3/simple/price', {
params: {
ids: 'usd',
vs_currencies: 'inr'
}
})
]);
const prices = cryptoResponse.data;
const usdToInrRate = conversionResponse.data.usd.inr;
const equivalentAmountsUSD = {
bitcoin: amountInUSD / prices.bitcoin.usd,
ethereum: amountInUSD / prices.ethereum.usd,
solana: amountInUSD / prices.solana.usd,
dogecoin: amountInUSD / prices.dogecoin.usd,
binancecoin: amountInUSD / prices.binancecoin.usd
};
const amountInINR = amountInUSD * usdToInrRate;
const equivalentAmountsINR = {
bitcoin: amountInINR / prices.bitcoin.usd,
ethereum: amountInINR / prices.ethereum.usd,
solana: amountInINR / prices.solana.usd,
dogecoin: amountInINR / prices.dogecoin.usd,
binancecoin: amountInINR / prices.binancecoin.usd
};
res.render('index', {
prices,
equivalentAmountsUSD,
equivalentAmountsINR,
amount: amountInUSD,
error: null
});
} catch (error) {
console.error(error);
res.render('index', {
prices: null,
equivalentAmountsUSD: null,
equivalentAmountsINR: null,
amount: null,
error: 'Failed to fetch data from API'
});
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});