If I'm serving a react application with npm install -g serve; serve -s build
, is it possible to also have a location proxy?
Ex: any requests to https://example.com/api/*
will be redirected to https://example.com:8000/api/*
If I'm serving a react application with npm install -g serve; serve -s build
, is it possible to also have a location proxy?
Ex: any requests to https://example.com/api/*
will be redirected to https://example.com:8000/api/*
You can try proxy in package.json
"proxy": {
"/api": {
"target": "http://localhost:3001"
},
"/assets": {
"target": "http://localhost:3001"
}
},
You may also check environment config options here if above does not solve: https://docs.npmjs.com/misc/config#proxy
As commented by sai anudeep, there is an open ticket about proxying requests but it has not seen any action in a while.
While developing you can define a proxy in package.json but that does not do anything for production builds.
If you just need something quick and easy you could go with local-web-server. You can even run it directly with npx
npm run build
npx local-web-server \
--port 8080 \
--directory build \
--spa index.html \
--rewrite '/api/(.*) -> http://localhost:4242/api/$1'
© 2022 - 2024 — McMap. All rights reserved.