-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (71 loc) · 2.55 KB
/
server.js
File metadata and controls
80 lines (71 loc) · 2.55 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
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 8080;
const API_URL = process.env.API_URL || 'http://localhost:8000';
// CORS middleware for development
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// Proxy /pash/api to the Pash backend
app.use('/pash/api', createProxyMiddleware({
target: API_URL,
changeOrigin: true,
pathRewrite: {
'^/pash/api': '', // Remove /pash/api prefix
},
onProxyReq: (proxyReq, req) => {
// Forward Authorization header
if (req.headers.authorization) {
proxyReq.setHeader('Authorization', req.headers.authorization);
}
},
logLevel: 'debug'
}));
// Serve Pash frontend (React app with client-side routing)
app.use('/pash', express.static(path.join(__dirname, 'pash'), {
setHeaders: (res, filePath) => {
// Don't cache index.html
if (filePath.endsWith('index.html')) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
} else {
// Cache assets for 1 year
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
}
}));
// Handle client-side routing for Pash - all /pash routes go to index.html
app.get('/pash/*', (req, res) => {
res.sendFile(path.join(__dirname, 'pash', 'index.html'));
});
// Serve main website static files
app.use(express.static(__dirname));
// Fallback for SPA routing in main site (if needed)
app.get('*', (req, res) => {
// Check if file exists
const filePath = path.join(__dirname, req.path);
if (require('fs').existsSync(filePath)) {
res.sendFile(filePath);
} else {
// Default to index.html
res.sendFile(path.join(__dirname, 'index.html'));
}
});
app.listen(PORT, () => {
console.log(`🌐 Server running at http://localhost:${PORT}`);
console.log(`📱 Main site: http://localhost:${PORT}`);
console.log(`🎵 Pash app: http://localhost:${PORT}/pash`);
console.log(`🔌 API proxy: http://localhost:${PORT}/pash/api → ${API_URL}`);
console.log('');
console.log('Make sure Pash backend is running:');
console.log(' cd /Users/sm1t/Code/pash/backend');
console.log(' source venv/bin/activate');
console.log(' uvicorn main:app --port 8000');
});