-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (51 loc) · 1.61 KB
/
server.js
File metadata and controls
66 lines (51 loc) · 1.61 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
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
const TARGET_URL = 'http://localhost:20001';
app.use(cors());
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
});
app.get('/health', (req, res) => {
res.json({
status: 'OK',
message: 'Proxy server is running',
target: TARGET_URL,
timestamp: new Date().toISOString()
});
});
const proxyOptions = {
target: TARGET_URL,
changeOrigin: true,
logLevel: 'info',
onProxyReq: (proxyReq, req, res) => {
console.log(`Proxying ${req.method} ${req.url} to ${TARGET_URL}`);
},
onProxyRes: (proxyRes, req, res) => {
console.log(`Received response from target: ${proxyRes.statusCode}`);
},
onError: (err, req, res) => {
console.error('Proxy error:', err.message);
res.status(500).json({
error: 'Proxy error',
message: err.message,
target: TARGET_URL
});
}
};
app.use('/', createProxyMiddleware(proxyOptions));
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(500).json({
error: 'Internal server error',
message: err.message
});
});
app.listen(PORT, () => {
console.log(`Proxy server running on http://localhost:${PORT}`);
console.log(`Forwarding requests to: ${TARGET_URL}`);
console.log(`Health check available at: http://localhost:${PORT}/health`);
});