Location Proxy with npm serve
Asked Answered
F

2

8

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/*

Fisc answered 4/5, 2018 at 2:30 Comment(1)
Tracking such request here - github.com/vercel/serve-handler/issues/30Imperil
F
1

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

Fireplug answered 4/5, 2018 at 2:36 Comment(0)
B
1

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'
Bidentate answered 30/11, 2023 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.