server.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const express = require('express');
  2. const path = require('path');
  3. const { createProxyMiddleware } = require('http-proxy-middleware');
  4. const app = express();
  5. const PORT = process.env.PORT || 3000;
  6. // Request logging middleware
  7. app.use((req, res, next) => {
  8. console.log(`[Frontend Server] ${req.method} ${req.url}`);
  9. next();
  10. });
  11. // Proxy API requests
  12. const backendUrl = process.env.BACKEND_URL || 'http://backend:5000';
  13. const proxyConfig = (pathPrefix) => ({
  14. target: `${backendUrl}${pathPrefix}`,
  15. changeOrigin: true,
  16. logLevel: 'debug',
  17. onProxyReq: (proxyReq, req, res) => {
  18. console.log(`[Proxy] Proxying ${req.method} ${req.url} -> ${backendUrl}${pathPrefix}${req.url}`);
  19. }
  20. });
  21. app.use('/auth', createProxyMiddleware(proxyConfig('/auth')));
  22. app.use('/restaurants', createProxyMiddleware(proxyConfig('/restaurants')));
  23. app.use('/admin', createProxyMiddleware(proxyConfig('/admin')));
  24. // Serve static files from the React app
  25. app.use(express.static(path.join(__dirname, 'client/dist')));
  26. app.get('*', (req, res) => {
  27. res.sendFile(path.join(__dirname, 'client/dist/index.html'));
  28. });
  29. app.listen(PORT, () => {
  30. console.log(`Server is running on port ${PORT}`);
  31. });