-1

I have a problem accessing my warp api from react, even though I have my vite proxy set up like so:

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:3030/api/shop',
        changeOrigin: false,
        secure: false,
        rewrite: path => path.replace('/api', ''),
      }
    },
  },
  plugins: [react()],
})

The warp api is set to allow any origins:

let cors = warp::cors()
    .allow_any_origin()
    .allow_methods(vec!["GET", "POST", "PATCH", "OPTIONS", "DELETE"]);

    warp::path!("api" / "shop" / "available")
        .and(warp::get())
        .map(|| {
            //Leftout logic...
            let json = json!({"available": available});
            Ok(warp::reply::json(&json))
        })
        .with(cors)

If I call my endpoints directly in browser ('http://localhost:3030/api/shop/available*) I get the desired output from my warp api but in case of calling the fetch from react I only get a 500 error cors problem and the warp endpoint is not even reached.

My rust server is a rewrite of an express server written in typescript, for the purpose of bettering my rust understanding. The previous express server didn't use any cors plugins and sends the exact same response.

Does anyone maybe know a solution? or anyone used react with vite & warp before?

Adjustment: Just turned on logging for my warp api and it seems like the endpoint is never even reached from the fetch call.

Chris
  • 1
  • 1
  • 1
    Could you include the snippet where you call the API from the vite code? Off the cuff it looks like your proxy target should be `http://localhost:3030/api/` not `http://localhost:3030/api/shop`, but it depends on what you're trying to do. – David T. Feb 01 '23 at 16:40
  • The endpoint is called `api/shop/available`, that's why it's configured like so. the fetch call is just a simple: `const res = await fetch("api/available")` – Chris Feb 01 '23 at 17:00
  • 1
    What does the network tab in the browser say the URL its trying to resolve is? – David T. Feb 01 '23 at 17:05

1 Answers1

0

I ot it to work after dockerizing the whole thing and changing te proxy from localhost to the container name.

Chris
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '23 at 09:22