-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
75 lines (62 loc) · 2.15 KB
/
Copy pathserver.ts
File metadata and controls
75 lines (62 loc) · 2.15 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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:8080';
// Middleware to parse JSON
app.use(express.json());
// Proxy logic for all Spring Boot API requests
app.all('/api/*', async (req, res) => {
const targetUrl = `${BACKEND_URL}${req.originalUrl}`;
console.log(`[Proxy] ${req.method} ${req.originalUrl} -> ${targetUrl}`);
try {
const headers = new Headers();
Object.entries(req.headers).forEach(([key, val]) => {
if (val !== undefined) {
if (Array.isArray(val)) {
val.forEach(v => headers.append(key, v));
} else {
headers.append(key, val);
}
}
});
const hasBody = req.method !== 'GET' && req.method !== 'HEAD';
const response = await fetch(targetUrl, {
method: req.method,
headers: headers,
body: hasBody ? JSON.stringify(req.body) : undefined,
});
const responseText = await response.text();
res.status(response.status);
// Copy target headers back to client response
response.headers.forEach((value, key) => {
// Avoid duplicate chunked encoding headers
if (key.toLowerCase() !== 'transfer-encoding') {
res.setHeader(key, value);
}
});
res.send(responseText);
} catch (error) {
console.error('[Proxy Error]:', error);
res.status(502).json({ error: 'Failed to contact backend server.' });
}
});
// Serve frontend static files in production
const distPath = path.join(__dirname, 'dist');
app.use(express.static(distPath));
// For all other requests, serve index.html (SPA Router support)
app.get('*', (req, res) => {
// If requesting an API that fell through
if (req.originalUrl.startsWith('/api')) {
return res.status(404).json({ error: 'Not Found' });
}
res.sendFile(path.join(distPath, 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});