-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (53 loc) · 1.6 KB
/
server.js
File metadata and controls
60 lines (53 loc) · 1.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
const express = require("express");
const app = express();
const port = 3000;
const mysql = require("mysql2");
// MySQL connection setup
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "CaptainAmerica27",
database: "munch_maps",
});
// Attempt to connect to MySQL database
connection.connect((err) => {
if (err) {
console.error("Error connecting to MySQL: ", err.message);
return;
}
console.log("Connected to MySQL database");
});
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Define route to fetch restaurants data by category, rating, distance, price, and service type
app.get("/restaurants", (req, res) => {
const { categoryType, ratingValue, distanceRange, priceRange, serviceType } = req.query;
let query = "SELECT * FROM restaurant WHERE 1=1";
if (categoryType) {
query += ` AND CategoryID = ${categoryType}`;
}
if (ratingValue) {
query += ` AND Rating_id = (SELECT Rating_id FROM rating WHERE Value = ${ratingValue})`;
}
if (distanceRange) {
query += ` AND DistanceID = ${distanceRange}`;
}
if (priceRange) {
query += ` AND PriceID = ${priceRange}`;
}
if (serviceType) {
query += ` AND ServiceTypeID = ${serviceType}`;
}
connection.query(query, (error, results) => {
if (error) {
console.error("Error fetching restaurants: ", error);
res.status(500).json({ error: "Internal server error" });
return;
}
res.json(results);
});
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});