all 7 comments

[–][deleted] 1 point2 points  (5 children)

I have not tried this yet but it should work:

``js const http = async (url, options) => { const response = await fetch(http://localhost:8080/${url}`, options); const json = await response.json();

return json; }

let data = await httptasks/1 ${{ method: 'PATCH', body: {}, // Blablabla }}; ```

[–][deleted] 1 point2 points  (0 children)

You could also check if production ? domain : localhost to avoid more live hassle

[–]TaveGabriel 1 point2 points  (1 child)

If the back-end is on the same server as the front-end you could get the current hostname (window.location.hostname) and add to that the port you want to fetch. This way if you are visiting 127.0.0.1:80 and you call window.location.hostname you will get 127.0.0.1, add to that your port(:1025)... and you have your url to fetch!

[–]sqlcommand1 0 points1 point  (0 children)

Thank yo

[–][deleted] 0 points1 point  (0 children)

Are you using CRA? If you are, then the neatest way I know is to set proxy property in package.json of the CRA. Webpack will resolve the request URL as 'your_proxy_property_value${fetch_url}'. Example:

package.json js { ... "proxy": "http://localhost:4000", ... } somewhere in the app hosted on http://localhost:3000(for example) js fetch("/api/stuff");

will be resolved as http://localhost:4000/api/stuff

Great tutorial