-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (76 loc) · 2.89 KB
/
Copy pathapp.js
File metadata and controls
90 lines (76 loc) · 2.89 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require('dotenv').config();
const express = require("express");
const { rateLimiterMiddleware } = require("./middleware/RateLimiterRedis");
const { authenticatePusher } = require("./middleware/pusherAuth");
const routes = require("./routes");
const session = require("cookie-session");
const { invalidateAllCaches } = require("./utils/cacheUtils");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 8000;
// Security headers middleware
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
next();
});
// Optimized CORS configuration - Pre-compute allowed origins
const ALLOWED_ORIGINS = [
process.env.FRONTEND_URL,
'https://app.pulseboard.co.in',
'https://www.pulseboard.co.in',
].filter(Boolean);
const corsOptions = {
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
// Use Set for O(1) lookup instead of O(n) indexOf
if (ALLOWED_ORIGINS.includes(origin)) {
callback(null, true);
} else {
// Remove console.warn to reduce overhead
callback(new Error('Not allowed by CORS'));
}
},
credentials: false, // Disable credentials to reduce preflight requests
optionsSuccessStatus: 200,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], // Remove OPTIONS (handled automatically)
allowedHeaders: ['Content-Type', 'Authorization'], // Minimize headers
};
// Use CORS with options in production, optimized for development
if (process.env.NODE_ENV === 'production') {
app.use(cors(corsOptions));
} else {
app.use(cors({
origin: "*",
credentials: false // Disable credentials in development too
}));
}
// Apply JSON parsing to all routes except webhooks
app.use('/api/v1/billing/webhook', express.raw({ type: 'application/json' }));
app.use(express.json({ limit: '10mb' })); // Add size limit
// Secure session configuration
app.use(
session({
secret: process.env.SESSION_SECRET || "your-session-secret-change-in-production",
resave: false,
saveUninitialized: false, // Changed to false for security
secure: process.env.NODE_ENV === 'production', // HTTPS only in production
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
})
);
// Apply rate limiting to all requests
app.use(rateLimiterMiddleware);
// Pusher authentication route
app.use("/api/v1/pusher/auth", authenticatePusher);
// All API routes
app.use("/api/v1", routes);
app.listen(port, () => {
console.log(`PulseBoard backend app listening on port ${port}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
});
// invalidateAllCaches();