-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (71 loc) · 2.57 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
require("dotenv").config({ path: "./backend/.env" });
const express = require("express");
const bodyParser = require('body-parser');
const session = require("express-session");
const passport = require("passport");
const appMiddlewares = require("./backend/middlewares/appMiddlewares");
const connectDB = require("./backend/models/db").connectDB;
const loginRouter = require("./backend/routes/loginRouter");
const registerRouter = require("./backend/routes/registerRouter");
const dashboardRouter = require("./backend/routes/dashboardRouter");
const logoutRouter = require("./backend/routes/logoutRouter");
const homeRouter = require("./backend/routes/homeRouter");
const authRouter = require("./backend/routes/authRouter");
const quizRouter = require("./backend/routes/quizRouter");
const rateLimiter = require('./backend/middlewares/rateLimiter');
const app = express();
//const { githubAuth, githubAuthCallback, githubAuthRedirect } = require('./backend/controllers/authController');
// app.use((req, res, next) => {
// console.log(`${req.method} ${req.url}`);
// next();
// });
// Apply rate limiting to all requests
//app.use(rateLimiter);
app.set('trust proxy', 1);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: false,
cookie: { secure: true} // Set to true if using HTTPS
}));
app.use(passport.initialize());
app.use(passport.session());
// Middleware setup
appMiddlewares(app);
// Apply rate limiting to specific routes
app.use('/login', rateLimiter);
//app.use('/register', rateLimiter);
//app.use('/auth/github', rateLimiter);
app.use((req, res, next) => {
console.log('Client IP:', req.ip);
next();
});
// Connect to MongoDB
connectDB()
.then(() => {
// Routes setup
app.use("/", homeRouter);
app.use("/", loginRouter);
app.use("/", registerRouter);
app.use("/", dashboardRouter);
app.use("/", logoutRouter);
app.use("/", authRouter);
app.use("/", quizRouter);
// app.get('/auth/github', githubAuth);
// app.get('/auth/github/callback', githubAuthCallback, githubAuthRedirect);
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((error) => {
console.error("Error connecting to database:", error);
});
// app.use((err, req, res, next) => {
// console.error('Error occurred:', err);
// res.status(500).send('Internal Server Error');
// });
// require('dotenv').config({ path: './.env' });