-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (102 loc) · 3.4 KB
/
index.js
File metadata and controls
121 lines (102 loc) · 3.4 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
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
// Importing required packages using ES module syntax
import express from "express";
import cors from "cors";
import rateLimit from "express-rate-limit";
import fetch from "node-fetch"; // Use node-fetch for fetch in Node.js
import dotenv from "dotenv";
dotenv.config(); // Load environment variables from .env
const API_KEY = process.env.API_KEY;
const app = express();
// CORS Configuration
const corsOptions = {
origin: process.env.CLIENT_DOMAIN, // Allow only the trusted client domain
methods: ["GET", "POST", "PUT", "DELETE"], // Specify allowed HTTP methods
allowedHeaders: ["Content-Type", "Authorization"], // Allowed headers
};
// Apply CORS middleware with the configured options
app.use(cors(corsOptions));
// Set up Rate Limiting - 100 requests per IP within a 15-minute window
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
message: "Too many requests from this IP, please try again later.",
});
// Apply rate limiting to all API requests
app.use(limiter);
// Example route to demonstrate external API request
app.get("/api/games", async (req, res) => {
const { search: searchQuery } = req.query;
// Ensure API_KEY is available
if (!API_KEY) {
return res
.status(500)
.json({ error: "API key not found in environment variables" });
}
try {
// Make a request to the external API using fetch
const response = await fetch(
`https://api.rawg.io/api/games?key=${API_KEY}&search=${searchQuery}`,
{
method: "GET",
headers: {
//Authorization: `Bearer ${API_KEY}`, some APIs requires using the authorization header but not this one
"Content-Type": "application/json",
},
}
);
if (!response.ok) {
throw new Error("Failed to fetch data from external API");
}
const data = await response.json(); // Parse the JSON response
// Send back the data received from the external API
return res.json(data);
} catch (error) {
console.error("Error fetching data from API:", error);
return res
.status(500)
.json({ error: "Failed to fetch data from external API" });
}
});
app.get("/api/game", async (req, res) => {
const { gameSlug } = req.query;
// Ensure API_KEY is available
if (!API_KEY) {
return res
.status(500)
.json({ error: "API key not found in environment variables" });
}
try {
// Make a request to the external API using fetch
const response = await fetch(
`https://api.rawg.io/api/games/${gameSlug}?key=${API_KEY}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
);
if (!response.ok) {
throw new Error("Failed to fetch data from external API");
}
const data = await response.json(); // Parse the JSON response
// Send back the data received from the external API
return res.json(data);
} catch (error) {
console.error("Error fetching data from API:", error);
return res
.status(500)
.json({ error: "Failed to fetch data from external API" });
}
});
// Basic route
app.get("/", (req, res) => {
res.send(
"Welcome to the Express App with CORS, Rate Limiting, and API Integration!"
);
});
// Set up the server to listen on a port
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});