-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro.js
More file actions
30 lines (23 loc) · 776 Bytes
/
micro.js
File metadata and controls
30 lines (23 loc) · 776 Bytes
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
import express, { Router } from 'express';
const app = express();
const router = Router();
const PORT = 8000;
const FIXER_API_URL =
'http://data.fixer.io/api/latest?access_key=5d35c596a45714ff94a4ed9321076f80&format=1';
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/', router);
router.route('/convert').get(routeHandler);
app.listen(PORT, () =>
console.log(`Example app listening on http://localhost:${PORT}`)
);
async function routeHandler(req, res) {
try {
const result = await fetch(FIXER_API_URL);
const json = await result.json();
res.json(json);
} catch (error) {
console.error("Error fetching data", error);
res.status(500).json({ error: "An error occured while fetching conversion data"})
}
}