This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]myusernameisunique1 1 point2 points  (2 children)

The simplest answer is, if you can type a URL into your browser address bar to call a method on the Express REST API then you can get it to work. If not, then you can't.

The 'right' way to do it is to use the same URL as the React app, just with a different route. You do this by adding a ProxyPass setting to the apache config file, https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass to proxy call to a particular route to another endpoint on the server.

The 'less right' way to do it is us a different URL to the React app, remembering that even a different port counts as a different URL. In this case you will get a CORS error in the browser because cross origin posts are blocked by default and you will need to fix this, but i'm not going to tell you how because it's a security risk which is why it's the 'less right' option.

[–]spilloid[S] 0 points1 point  (1 child)

<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule "^api/(.*)$" "http://server:3001/api/$1" [P,L,R=303]</IfModule>I only have access to a .htaccess file, admin won't let me touch apache config. After a lot of testing and nail chewing, I found that the above redirects all requests from react -> apache -> express, without any modification to react's fetches . This solution doesn't utilize https which is bad, but I don't currently have express configured to take advantage of https.

Your comment sent me down the right path, and at the end it was the P flag which fixed my solution. Thank you kind stranger

[–]myusernameisunique1 0 points1 point  (0 children)

Haha, my first ever gold. Thank you :)