forked from rajzzz/Country-Stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (24 loc) · 889 Bytes
/
Copy pathserver.js
File metadata and controls
28 lines (24 loc) · 889 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
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
const app = express();
const PORT = 3001;
app.use(cors());
app.get('/api/country/:name', async (req, res) => {
const countryName = req.params.name;
const apiUrl = `https://data.un.org/ws/rest/v1/country/${encodeURIComponent(countryName)}`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
return res.status(response.status).send(`Failed to fetch data for ${countryName}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching country stats:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(PORT, () => {
console.log(`Proxy server running at http://localhost:${PORT}`);
});