Reverse Proxy Create React App Build with Backend NodeJS Express
const express = require('express');
const proxy = require('http-proxy-middleware');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, '../build')));
const backendURL = process.env.REACT_APP_BACKEND_HOST;
const options = {
target: backendURL, // target host
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
};
const customProxy = proxy(options);
app.use('/api', customProxy);
app.get('/*', function(request, response, next) {
response.sendFile(path.join(__dirname, '../build', 'index.html'));
});
const port = process.env.PORT || 3000;
app.listen({ port }, () =>
console.log(`🚀 🚀 🚀 🚀 Server ready at http://localhost:${port} `)
);