-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (67 loc) · 1.9 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
import dotenv from "dotenv";
import express from "express";
import cookieParser from "cookie-parser";
import cors from "cors";
import morgan from "morgan";
import rateLimit from "express-rate-limit";
import process from "process";
import { connectToDatabase } from "./src/utils/db.js";
import {
signIn,
signUp,
checkEmail,
changePassword,
insertUser,
getAllUsers,
deleteUser,
} from "./src/controllers/authController.js";
import {
authMiddleware,
errorHandler,
} from "./src/middleware/authMiddleware.js";
dotenv.config();
const app = express();
app.use(morgan("combined"));
app.use(express.json());
app.use(cookieParser());
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
});
app.use(limiter);
const whitelist = ["http://localhost:5173"];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
};
app.use(cors(corsOptions));
app.get("/", (req, res) => res.send("Welcome to the Express server!"));
app.get("/insert", insertUser);
app.get("/users", getAllUsers);
app.post("/signin", signIn);
app.post("/signup", signUp);
app.post("/api/check-email", checkEmail);
app.post("/api/change-password", authMiddleware, changePassword);
app.get("/allowedaccess", authMiddleware, (req, res) =>
res.send("This is a protected route")
);
app.delete("/api/delete-user", authMiddleware, deleteUser);
app.get("/logout", (req, res) => {
res.clearCookie("token");
res.send("Logged out successfully");
});
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
async function startServer() {
await connectToDatabase();
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
}
startServer();