-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (55 loc) · 2.01 KB
/
index.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
//Import Express and Axios
import express, { json } from "express";
import axios from "axios";
//Create an express app, set the port number, and the API URL
const app = express();
const port = 3000;
const API_Link = "https://api.openbrewerydb.org/v1/breweries";
//middleware to parse the request body
express.urlencoded({ extended: true });
//Use the public folder for static files
app.use(express.static("public"));
//When the user goes to the home page it should render the index.ejs file
app.get("/", (req, res) => {
res.render("index.ejs");
});
//When the user goes to the /get-breweries page, it should get the breweries from the API and render the index.ejs file with the breweries
app.get("/get-breweries", async (req, res) => {
const {by_city, by_state, by_postal} = req.query;
let API_URL = `${API_Link}?`;
if (by_city) {
API_URL += `by_city=${by_city}&`;
}
if (by_state) {
API_URL += `by_state=${by_state}&`;
}
if (by_postal) {
API_URL += `by_postal=${by_postal}&`;
}
// Remove the trailing '&' if it exists
if (API_URL.endsWith('&')) {
API_URL = API_URL.slice(0, -1);
}
console.log(API_URL);
try {
const result = await axios.get(API_URL);
const breweries = result.data.map(brewery => {
if (brewery.phone) {
const cleaned = ('' + brewery.phone).replace(/\D/g, '');
const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
brewery.phone = '(' + match[1] + ') ' + match[2] + '-' + match[3];
}
}
return brewery;
});
res.render("index.ejs", {breweries});
} catch (error) {
console.log(error);
res.render("index.ejs", { error: 'Failed to get breweries' });
}
});
//Listen on your predefined port and start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});