Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
119 lines (98 loc) · 3.36 KB
/
Copy pathserver.js
File metadata and controls
119 lines (98 loc) · 3.36 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
import express from 'express';
import cors from 'cors';
import http from 'http';
import https from 'https';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3001;
const TARGET_URL = new URL('https://pol.is');
// Simple in-memory cache
const cache = new Map();
const CACHE_DURATION = 30 * 1000; // 30 seconds in milliseconds
// Enable CORS
app.use(cors());
// Simple logger middleware
app.use((req, res, next) => {
console.log('\n---- INCOMING REQUEST ----');
console.log(`${req.method} ${req.url}`);
console.log('Request Headers:');
console.log(req.headers);
// Capture response headers
const originalSend = res.send;
res.send = function(...args) {
console.log('\n---- OUTGOING RESPONSE ----');
console.log(`Status: ${res.statusCode}`);
console.log('Response Headers:');
console.log(res.getHeaders());
return originalSend.apply(this, args);
};
next();
});
// Serve static files from the build directory with cache control
app.use(express.static(path.join(__dirname, 'build'), {
etag: true,
lastModified: true,
setHeaders: (res, path) => {
// For JS, CSS, and HTML files - no cache (must revalidate)
if (path.endsWith('.js') || path.endsWith('.css') || path.endsWith('.html')) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
} else {
// For other static assets - cache for 1 hour
res.setHeader('Cache-Control', 'max-age=3600');
}
}
}));
// Custom proxy implementation
app.use('/proxy', async (req, res) => {
try {
// Remove '/proxy' prefix
const path = req.url.replace(/^\/proxy/, '') || '/';
const url = new URL(path, TARGET_URL).toString();
console.log(`Proxying request to: ${url}`);
// Check cache for GET requests
if (req.method === 'GET') {
const cacheKey = url;
const cachedResponse = cache.get(cacheKey);
if (cachedResponse && cachedResponse.expiry > Date.now()) {
console.log('Serving response from cache');
res.status(cachedResponse.status);
return res.send(cachedResponse.data);
}
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Get full response data
const data = await response.text();
console.log('Data fetched successfully!');
console.log(`First 200 characters of data: ${data.substring(0, 200)}...`);
// Cache the response for GET requests
if (req.method === 'GET') {
const cacheKey = url;
cache.set(cacheKey, {
data,
status: response.status,
expiry: Date.now() + CACHE_DURATION
});
console.log(`Cached response for ${cacheKey}`);
}
res.status(response.status);
res.send(data);
} catch (error) {
console.error('Proxy error:', error);
res.status(500).send('Proxy Error: ' + error.message);
}
});
// Handle any remaining requests with index.html (SPA routing)
app.get('*', (req, res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});