forked from suvomx1999/FirstContact
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (54 loc) · 1.65 KB
/
Copy pathserver.js
File metadata and controls
71 lines (54 loc) · 1.65 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
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const connectDB = require('./config/db');
const errorHandler = require('./middleware/error');
// Security Middleware
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require('helmet');
const xss = require('xss-clean');
const rateLimit = require('express-rate-limit');
const hpp = require('hpp');
// cors was already declared at the top, removing duplicate declaration here
const path = require('path');
// Load environment variables
dotenv.config();
// Connect to Database
connectDB();
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.json()); // Body parser for JSON
// Serve static files
app.use(express.static(path.join(__dirname)));
// Sanitize data
// app.use(mongoSanitize()); // Disabled due to compatibility issue with latest Express/Mongoose versions in this environment
// Set security headers
app.use(helmet());
// Prevent XSS attacks
// app.use(xss()); // Disabled due to compatibility with newer Express versions
// Rate limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 mins
max: 100
});
app.use(limiter);
// Prevent http param pollution
app.use(hpp());
// Enable CORS
app.use(cors());
// Routes
app.use('/api/auth', require('./routes/auth'));
// Health Check API
app.get('/api/health', (req, res) => {
res.status(200).json({
success: true,
message: 'System operational',
timestamp: new Date().toISOString()
});
});
app.use(errorHandler);
// Start Server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});