How to Write Fetch URLs when working on a local test?

I’m working on a project using React and Express. What is the best practice for coding in the fetch URLs that my react project will use when those same URLs will change in production?

i.e., they’re localhost:8000/getdata (or whatever) right now, but they’ll be someapp.com/getdata when it goes to production?

I’m sure there’s some kind of best practice for this, but I don’t even know what it’s called to google it.

Thank you!

1 Like

Will the fetch calls not be on the same domain as the React app resides?

1 Like

They aren’t currently–I’m running a react instance and a separate Express one. Most of the tutorials I’ve seen indicate that express has a hard time running anything but a npm build version a react page. I would LIKE to have the react page be served up as the static page from express when I’m done, though.

EDIT: slight change for clarity

1 Like

For a simple light traffic site, Express will work fine for serving static files (index.html, index.js, images, etc.) For moderate to heavy traffic sites, I would use Nginx to serve all the static pages (including the React app) and Express for the backend. This is how I do all of my sites.

You can read more about this in this article. There is a bit a learning curb for Nginx, but it is very fast and powerful.

Hi there,

Wonderful, that’s just the kind of info I’m looking for (the separation of back and front end, when it’s appropriate, etc.). But the link didn’t come through–can you please resend?

Thank you!

Sorry about that. I never hit paste.