-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (30 loc) · 1.14 KB
/
Copy pathserver.js
File metadata and controls
36 lines (30 loc) · 1.14 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
const express = require('express');
const httpProxy = require('http-proxy');
const path = require('path');
const app = express();
const proxy = httpProxy.createProxyServer({});
// Serve static files from the build folder at /front/
app.use('/front', express.static(path.join(__dirname, 'build')));
// Proxy API requests from /api/* to http://localhost:8000/
app.all('/api/*path', (req, res) => {
// Rewrite the URL to remove /api/ and keep the rest
//req.url = req.url.replace(/^\/api/ '');
proxy.web(req, res, { target: 'http://backend:8000' }, (err) => {
console.error('Proxy error:', err);
res.status(500).send('Proxy error');
});
});
// Handle SPA routing: Serve index.html for all routes under /front/*
app.get('/front/*path', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// Handle proxy errors
proxy.on('error', (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).send('Proxy error');
});
const port = 3000;
app.listen(port, () => {
console.log(`App served at http://localhost:${port}/front/`);
console.log(`API requests proxied from /api/ to http://localhost:8000/`);
});