-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (72 loc) · 2.6 KB
/
index.js
File metadata and controls
82 lines (72 loc) · 2.6 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
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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
try {
//Fetch
const URL = `https://restcountries.com/v3.1/region/asia`;
const response = await axios.get(URL);
//Get random country
const list = response.data;
const idx = Math.floor(Math.random() * list.length);
const countryObj = list[idx];
const country = countryObj.name.common;
const capital =countryObj.capital[0];
const languagekey = Object.keys(countryObj.languages || {})[0];
const language = countryObj.languages[languagekey];
const currencyKey = Object.keys(countryObj.currencies || {})[0];
const currency = countryObj.currencies[currencyKey].name;
console.log(country);
console.log(capital);
console.log(language);
console.log(currency);
const countryData = {country, capital, language, currency};
//Render the country data to the index.ejs
res.render("index.ejs", { data: countryData });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: error.message,
});
}});
app.post("/", async (req, res) => {
console.log(req.body);
console.log(req.body.type);
try {
//Fetch
var userRegion = req.body.type;
const URL = `https://restcountries.com/v3.1/region/${userRegion}`;
const response = await axios.get(URL);
//Get random country
const continent = req.body.type.slice(0, 1).toUpperCase() + req.body.type.slice(1);
const list = response.data;
const idx = Math.floor(Math.random() * list.length);
const countryObj = list[idx];
const country = countryObj.name.common;
const capital =countryObj.capital[0];
const languagekey = Object.keys(countryObj.languages || {})[0];
const language = countryObj.languages[languagekey];
const currencyKey = Object.keys(countryObj.currencies || {})[0];
const currency = countryObj.currencies[currencyKey].name;
console.log(country);
console.log(capital);
console.log(language);
console.log(currency);
const countryData = {continent, country, capital, language, currency};
//Render
res.render("index.ejs", { data: countryData });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: error.message,
});
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});