-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 984 Bytes
/
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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const rateLimiter = require("./middlewares/rateLimiter");
const weatherRoutes = require("./routes/weather");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
const whiteList = [
"http://127.0.0.1",
"http://127.0.0.1:5500",
"http://localhost",
"http://localhost:5500",
"https://billalben.github.io",
"https://weather-io-app.netlify.app",
];
const corsOptions = {
origin: (origin, callback) => {
if (!origin || whiteList.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not Allowed By CORS"));
}
},
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
app.use(rateLimiter);
app.get("/", (req, res) => {
res.json({ message: "Welcome to the API" });
});
app.use("/weather", weatherRoutes);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});