-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.8 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
const express = require('express');
const helmet = require('helmet');
const morgan = require('morgan');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const xss = require('xss-clean');
const mongoSanitize = require('express-mongo-sanitize');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const authRoutes = require('./routes/authRoutes');
const verifyRoles = require("./routes/verifyRoles");
const { errorHandler } = require('./middlewares/errorMiddleware');
const ballotRoutes = require('./routes/ballotRoutes');
const voteRoutes = require('./routes/voteRoutes');
const resultRoutes = require('./routes/resultRoutes');
// Load environment variables
require('dotenv').config();
// Database connection
require("./config/db").connectDB();
// Initialize express app
const app = express();
// Middleware setup
app.use(express.json());
app.use(helmet());
app.use(morgan('common'));
app.use(cors({ origin: process.env.CLIENT_URL, credentials: true }));
app.use(rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: {
success: false,
message: 'Too many requests, please try again later.'
}
})); // 15 minutes, 100 requests
app.use(xss());
app.use(mongoSanitize());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
if (process.env.NODE_ENV === 'production') {
app.set('trust proxy', 1); // trust first proxy
}
// Routes
app.use('/api/auth', authRoutes);
app.use('/api', verifyRoles);
app.use('/api/ballots', ballotRoutes);
app.use('/api/votes', voteRoutes);
app.use('/api', resultRoutes);
// Error handler middleware
app.use(errorHandler);
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));