-
Notifications
You must be signed in to change notification settings - Fork 30
/
server.js
102 lines (90 loc) · 3.11 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const express = require("express");
const mysql = require("mysql2");
const fs = require("fs");
require("dotenv").config();
const app = express();
// Create a connection pool to the database
const pool = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
port: 3306,
ssl: { ca: fs.readFileSync("./DigiCertGlobalRootCA.crt.pem") },
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
//========== ENDPOINTS ============//
// Root endpoint: returns all countries
app.get("/", (req, res) => {
console.log("GET / endpoint was hit 🎯");
pool.query("SELECT * FROM country", (err, results) => {
if (err) {
console.error("Error fetching countries:", err);
return res.status(500).send("Internal Server Error");
}
res.json(results);
});
});
// Get a list of countries that belong to Oceania
app.get("/oceania", (req, res) => {
console.log("GET /oceania endpoint was hit 🎯");
const query = `
SELECT Name, LifeExpectancy
FROM country
WHERE Continent = 'Oceania'
ORDER BY LifeExpectancy DESC
LIMIT 10;
`;
pool.query(query, (err, results) => {
if (err) {
console.error("Error fetching Oceania countries:", err);
return res.status(500).send("Internal Server Error");
}
res.json(results);
});
});
// Show information about a specific country by its name
app.get("/country/:countryname", (req, res) => {
console.log(`GET /country/${req.params.countryname} endpoint was hit 🎯`);
const { countryname } = req.params;
const query = "SELECT * FROM country WHERE Name = ?";
pool.execute(query, [countryname], (err, results) => {
if (err) {
console.error(`Error fetching details for ${countryname}:`, err);
return res.status(500).send("Internal Server Error");
}
res.json(results);
});
});
// Find the total population of a continent
app.get("/population/:continent", (req, res) => {
console.log(`GET /population/${req.params.continent} endpoint was hit 🎯`);
const { continent } = req.params;
const query = "SELECT SUM(Population) AS total_population FROM country WHERE Continent = ?";
pool.execute(query, [continent], (err, results) => {
if (err) {
console.error(`Error fetching population for ${continent}:`, err);
return res.status(500).send("Internal Server Error");
}
const totalPopulation = results[0]?.total_population;
res.send(`The total population of ${continent} is ${totalPopulation}`);
});
});
//========== PORT ============//
/**
App Service sets the environment variable PORT in the Node.js container,
and forwards the incoming requests to your container at that port number.
To receive the requests, your app should listen to that port using process.env.PORT.
*/
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server is live at http://localhost:${PORT}`);
}).on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error("Port is already in use");
} else {
console.error("Server Error:", error);
}
});