-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-server.js
More file actions
85 lines (75 loc) · 2.4 KB
/
Copy pathproxy-server.js
File metadata and controls
85 lines (75 loc) · 2.4 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
/**
* Development proxy server for CouchDB
* This proxy helps avoid CORS issues during development
*/
import { createProxyMiddleware } from 'http-proxy-middleware';
import express from 'express';
import cors from 'cors';
const app = express();
const PORT = process.env.PROXY_PORT || 3001;
const COUCHDB_URL = process.env.COUCHDB_URL || 'http://localhost:5984';
// Enable CORS for all routes
app.use(cors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'X-Requested-With']
}));
// Proxy middleware configuration
const proxyOptions = {
target: COUCHDB_URL,
changeOrigin: true,
logLevel: 'debug',
onProxyReq: (proxyReq, req, res) => {
console.log(`[PROXY] ${req.method} ${req.url} -> ${COUCHDB_URL}${req.url}`);
},
onProxyRes: (proxyRes, req, res) => {
// Ensure CORS headers are set
proxyRes.headers['Access-Control-Allow-Origin'] = req.headers.origin || '*';
proxyRes.headers['Access-Control-Allow-Credentials'] = 'true';
proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, HEAD, OPTIONS';
proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, Accept, Origin, X-Requested-With';
},
onError: (err, req, res) => {
console.error('[PROXY ERROR]', err.message);
res.status(500).json({
error: 'Proxy Error',
message: err.message,
target: COUCHDB_URL
});
}
};
// Create proxy middleware
const proxy = createProxyMiddleware(proxyOptions);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'ok',
proxy: 'CouchDB Development Proxy',
target: COUCHDB_URL,
timestamp: new Date().toISOString()
});
});
// Proxy all other requests to CouchDB
app.use('/', proxy);
// Start the proxy server
app.listen(PORT, () => {
console.log(`
🚀 CouchDB Development Proxy Server
===================================
Proxy URL: http://localhost:${PORT}
Target: ${COUCHDB_URL}
Health Check: http://localhost:${PORT}/health
Use this proxy URL in your application configuration:
commad.config.setCouchDB("http://localhost:${PORT}")
`);
});
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('Shutting down proxy server...');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('Shutting down proxy server...');
process.exit(0);
});