Yosia
Yosia

Reputation: 69

Setting Up base url for production while using proxy in development in React

I'm setting up a new project. In it I used a proxy settings inside my package.json.

Now, everytime I use fetch I do this:

fetch("/foo")

instead of this:

fetch("http://www.bar.com/foo")

This is all good while I'm in development mode. However, I dont know how can I supply my fetch() with the appropriate base url while my app is in production mode.

Any help is much appreciated. Thank you!

Upvotes: 0

Views: 1884

Answers (1)

tarzen chugh
tarzen chugh

Reputation: 11257

1) Create config.json and add base url key value pair in json format as shown below.

{
  "baseUrl": "http://www.bar.com/",
}

2) For CRUD operations through fetch or any other library use baseUrl from config. You could change baseUrl using webpack while building for different environments like production or development.

Hope that helps!!

Upvotes: 1

Related Questions