-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (77 loc) · 2.29 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
const express = require("express");
const cors = require("cors");
const fetch = require("cross-fetch");
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
// For Restaurant API
app.get("/api/restaurants", async (req, res) => {
const { lat, lng, page_type } = req.query;
console.log(req.query);
const url = `https://www.swiggy.com/dapi/restaurants/list/v5?lat=${lat}&lng=${lng}&page_type=${page_type}`;
await fetch(url, {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log(data);
res.json(data);
})
.catch((error) => {
console.error(error);
res.status(500).send("An error occurred");
});
});
// For Menu API
app.get("/api/menu", async (req, res) => {
const {
"page-type": page_type,
"complete-menu": complete_menu,
lat,
lng,
submitAction,
restaurantId,
} = req.query;
console.log(req.query);
const url = `https://www.swiggy.com/dapi/menu/pl?page-type=${page_type}&complete-menu=${complete_menu}&lat=${lat}&lng=${lng}&submitAction=${submitAction}&restaurantId=${restaurantId}`;
await fetch(url, {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log(data);
res.json(data);
})
.catch((error) => {
console.error(error);
res.status(500).send("An error occurred");
});
});
app.get("/", (req, res) => {
res.json({
test: "Welcome to MealMingle! - See Live Web URL for this Server - https://mealmingle-app.netlify.app/",
});
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});