-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (116 loc) · 4.26 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const express = require("express");
const cors = require("cors");
const PORT = process.env.PORT || 3000;
const axios = require("axios");
const { createProxyMiddleware } = require("http-proxy-middleware");
const { mobileApiUrl } = require("./Constants.JS");
const { pcApiUrl } = require("./Constants.JS");
const app = express();
// Allow requests from anywhere
app.use(cors());
// Proxy route
app.use("/api/v1/mobile", (req, res, next) => {
const proxyMiddleware = createProxyMiddleware({
target: mobileApiUrl,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
proxyReq.setHeader(
"User-Agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
);
},
});
proxyMiddleware(req, res, next);
});
app.get("/api/v1/mobile", async (req, res) => {
try {
const response = await axios.get(mobileApiUrl);
res.json(response.data);
} catch (error) {
console.error("Error fetching data:", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.use("/api/v1/pc", (req, res, next) => {
const proxyMiddleware = createProxyMiddleware({
target: pcApiUrl,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
proxyReq.setHeader(
"User-Agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
);
},
});
proxyMiddleware(req, res, next);
});
app.get("/api/v1/pc", async (req, res) => {
try {
const response = await axios.get(pcApiUrl);
res.json(response.data);
} catch (error) {
console.error("Error fetching data:", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.use("/api/v1/restaurantmenu/:id", (req, res, next) => {
const resId = req.params.id;
const targetUrl = `https://www.swiggy.com/mapi/menu/pl?page-type=REGULAR_MENU&complete-menu=true&lat=28.6542&lng=77.2373&restaurantId=${resId}&isMenuUx4=true&submitAction`;
const proxyMiddleware = createProxyMiddleware({
target: targetUrl,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
proxyReq.setHeader(
"User-Agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
);
},
});
proxyMiddleware(req, res, next);
});
app.get("/api/v1/restaurantmenu/:id", async (req, res) => {
const resId = req.params.id;
const url = `https://www.swiggy.com/mapi/menu/pl?page-type=REGULAR_MENU&complete-menu=true&lat=28.6542&lng=77.2373&restaurantId=${resId}&isMenuUx4=true&submitAction`;
try {
const response = await axios.get(url);
res.json(response.data);
} catch (error) {
console.error("Error fetching data:", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.use("/api/v1/collections/:type/:id", (req, res, next) => {
const collectionId = req.params.id;
const collectionType = req.params.type;
const targetUrl = `https://www.swiggy.com/dapi/restaurants/list/v5?lat=28.6542&lng=77.2373&collection=${collectionId}&tags=${collectionType}&sortBy=&filters=&type=rcv2&offset=0&page_type=null`;
const proxyMiddleware = createProxyMiddleware({
target: targetUrl,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
proxyReq.setHeader(
"User-Agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 15_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
);
},
});
proxyMiddleware(req, res, next);
});
app.get("/api/v1/collections/:type/:id", async (req, res) => {
const collectionId = req.params.id;
const collectionType = req.params.type;
const url = `https://www.swiggy.com/dapi/restaurants/list/v5?lat=28.6542&lng=77.2373&collection=${collectionId}&tags=${collectionType}&sortBy=&filters=&type=rcv2&offset=0&page_type=null`;
try {
const response = await axios.get(url);
res.json(response.data);
} catch (error) {
console.error("Error fetching data:", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/", (req, res) => {
res.send("Server Running");
});
// Server Starts
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});