-
When using CRA for production build, the get requests should get proxied but instead they are sent to express server (static server) to get the index.html file. Below the files used in the express server (middlewares and routes) Middleware: const proxy = require('express-http-proxy')
function middlewares(app) {
app.use(
'/api',
proxy(serviceUrl, {
proxyReqPathResolver: (req) =>
serviceUrl + req.url,
limit: '512mb',
})
)
} Express routes: const express = require('express')
function routes(app) {
// Healthcheck
app.get('/healthcheck', (req, res) => {
res.send('OK')
})
// Proxy static resources
app.use(express.static(path.resolve(__dirname, '../../build')))
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../../build/index.html'))
})
}
module.exports = routes Static server: const app = require('express')()
export const startServer = () => {
const app = express();
middlewares(app);
routes(app);
// Listen
app.listen(
port,
() => console.log(`Listening on :${port} ...`),
);
}; but when I am making a get request using fetch it ends up getting the index.html instead of getting proxied to the service url. All POST requests get proxied. What am I missing here and how to fix this? TIA |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I had by mistake added routes before middleware hence this issue. Closing this. |
Beta Was this translation helpful? Give feedback.
I had by mistake added routes before middleware hence this issue. Closing this.