-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (85 loc) · 3.36 KB
/
server.js
File metadata and controls
102 lines (85 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
import express from 'express'
import path from 'path'
import { fileURLToPath } from 'url'
import { createProxyMiddleware } from 'http-proxy-middleware'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const port = process.env.PORT || 4173
const upstream = 'https://cubing-tw.net'
app.options('/api/{0,}', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
res.setHeader('Access-Control-Max-Age', '86400')
res.status(204).end()
})
// ⭐ API Proxy - MUST come before static files and catch-all
app.use(
'/api',
createProxyMiddleware({
target: upstream,
changeOrigin: true,
secure: true,
pathRewrite: (pathStr) => {
// Map /api/competitors -> /event/2025TaiwanChampionship/competitors
if (pathStr.startsWith('/competitors')) {
return pathStr.replace(
/^\/competitors/,
'/event/2025TaiwanChampionship/competitors'
)
}
// Map /api/events -> /event/2025TaiwanChampionship/event
if (pathStr.startsWith('/events')) {
return pathStr.replace(
/^\/events/,
'/event/2025TaiwanChampionship/event'
)
}
return pathStr
},
onProxyReq: (proxyReq, req, res) => {
// Make the upstream server think the request came from its own domain
proxyReq.setHeader('Origin', upstream)
proxyReq.setHeader('Referer', upstream)
proxyReq.removeHeader('Host')
console.log('🔵 Proxying:', req.method, req.path, '→', proxyReq.path)
},
// ⭐ CRITICAL: Add CORS headers to response
onProxyRes: (proxyRes, req, res) => {
proxyRes.headers['access-control-allow-origin'] = '*'
proxyRes.headers['access-control-allow-methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
proxyRes.headers['access-control-allow-headers'] = 'Content-Type, Authorization, X-Requested-With'
proxyRes.headers['access-control-allow-credentials'] = 'true'
// Remove potentially problematic headers
delete proxyRes.headers['x-frame-options']
delete proxyRes.headers['content-security-policy']
console.log('✅ Proxied response:', req.path, '→ Status:', proxyRes.statusCode)
},
onError: (err, req, res) => {
console.error('❌ Proxy error:', err. message)
console.error(' Path:', req.path)
console.error(' Method:', req.method)
res.status(502).json({
error: 'Bad Gateway',
message: 'Failed to proxy request to upstream server',
details: process.env.NODE_ENV === 'development' ? err.message : undefined,
path: req.path
})
},
logLevel: process.env.NODE_ENV === 'development' ? 'debug' : 'warn',
})
)
const distPath = path.join(__dirname, 'dist')
app.use(express.static(distPath))
app.get('{0,}', (req, res) => {
// Safeguard: Never serve index.html for API routes
if (req.path.startsWith('/api/')) {
return res.status(404).json({ error: 'API endpoint not found' })
}
console.log('📄 Serving SPA for:', req.path)
res.sendFile(path.join(distPath, 'index.html'))
})
app.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`)
})