-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (104 loc) · 3.25 KB
/
Copy pathserver.js
File metadata and controls
120 lines (104 loc) · 3.25 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const compression = require('compression');
const morgan = require('morgan');
const app = express();
const PORT = process.env.PORT || 3000;
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com", "https://cdnjs.cloudflare.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com", "https://cdnjs.cloudflare.com"],
scriptSrc: ["'self'", "https://unpkg.com", "https://cdnjs.cloudflare.com"],
connectSrc: ["'self'", "https://unpkg.com", "https://cdnjs.cloudflare.com"],
imgSrc: ["'self'", "data:"],
objectSrc: ["'none'"],
baseUri: ["'self'"],
formAction: ["'self'"],
},
},
crossOriginEmbedderPolicy: false
}));
// CORS configuration
app.use(cors({
origin: process.env.NODE_ENV === 'production'
? process.env.FRONTEND_URL || false
: true,
credentials: true
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: {
error: 'Too many requests from this IP, please try again later.',
retryAfter: '15 minutes'
},
standardHeaders: true,
legacyHeaders: false,
});
// Compression middleware
app.use(compression());
// Request logging
if (process.env.NODE_ENV !== 'test') {
app.use(morgan('combined'));
}
// Apply rate limiting to all routes
app.use(limiter);
// Body parsing middleware for JSON (useful for future API endpoints)
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Serve static files from the current directory with caching headers
app.use(express.static(__dirname, {
maxAge: process.env.NODE_ENV === 'production' ? '1d' : 0,
etag: true
}));
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV || 'development'
});
});
// Handle all routes by serving index.html (for SPA behavior)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Error occurred:', err);
res.status(500).json({
error: process.env.NODE_ENV === 'production'
? 'Internal server error'
: err.message
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
error: 'Route not found',
path: req.originalUrl
});
});
app.listen(PORT, () => {
console.log(`🚀 PDF Compressor server is running on port ${PORT}`);
console.log(`📊 Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`🔗 Visit: http://localhost:${PORT}`);
console.log(`❤️ Health check: http://localhost:${PORT}/health`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
process.exit(0);
});