-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
34 lines (26 loc) · 910 Bytes
/
server.js
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
const express = require('express');
const path = require('path');
const httpProxy = require('http-proxy');
const app = express();
const proxy = httpProxy.createProxyServer();
const PORT = 3000;
const API_BACKEND_URL = 'http://gamesite-backend:3000/api';
// Serve static files for React production app
app.use(express.static(path.join(__dirname, 'build')));
// Forward API requests to the backend server
app.use('/api', (req, res) => {
proxy.web(req, res, { target: API_BACKEND_URL });
});
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// Handle proxy errors
proxy.on('error', (err, req, res) => {
console.error(err);
res.status(500).json({ message: "Server error", errors: [{ msg: 'Server error' }] });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});