-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (39 loc) · 1.33 KB
/
server.js
File metadata and controls
54 lines (39 loc) · 1.33 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
// import Express library and activate it
import express from "express";
const app = express();
// import path module to help with file paths
import path from 'path';
// Serve static files from the 'public' folder
app.use(express.static('public'))
// On Vercel, point the root url (/) to index.html explicitly
if (process.env.VERCEL) {
app.get('/', (req, res) => {
res.sendFile(path.join(process.cwd(), 'public', 'index.html'))
})
}
// publish the current datetime as an API endpoint
app.get("/api/datetime", (req, res) => {
res.send({
datetime: new Date()
})
});
app.get("/api/ethereum", async (req, res) => {
const today = new Date()
const monthAgo = new Date()
monthAgo.setMonth(monthAgo.getMonth() - 1)
let url = new URL('https://rest.coinapi.io/v1/exchangerate/ETH/CAD/history')
url.searchParams.set('apikey', process.env.APIKEY)
url.searchParams.set('period_id', '1DAY')
url.searchParams.set('time_start', monthAgo.toISOString())
url.searchParams.set('time_end', today.toISOString())
url.searchParams.set('limit', 31)
const response = await fetch(url.href)
const data = await response.json()
console.log(data)
res.send(data)
});
const port = 3000
// Start Express
app.listen(port, () => {
console.log(`Express is Live at http://localhost:${port}`);
});