Elaine Chen
Elaine Chen

Reputation: 197

Switch from parcel v1 -> CRA, how to serve static web file with reload during development

Backstory: I was originally going to switch to parcel v2, ran into a lot of issues, and really starting to dislike parcel. I managed to resolve some issues even though their error messages are very unintuitive. Got to a point that "parcel build" is working but "parcel serve" just doesn't, and can't find an answer online. At that point, I decided to switch to Create React App because it's more "industry" standard. (Thank you for listening to me rant.)

When I was using parcel v1, my setup for the dev environment is running "parcel index.html" (which has hot module replacement), and I'm serving the static files in my backend.

But I don't know how to do that with create-react-script. "react-scripts start" doesn't build to the "build" folder, "react-scripts build" only build once and no reload. I still want to serve the static file. What should I do?

Upvotes: 1

Views: 174

Answers (1)

Joshua
Joshua

Reputation: 2682

If you're just doing this for development purposes, you could use a proxy from your backend server, pointing to the server run by the Create React App.

For example, with Fastify, you can serve your React app on 1234, and proxy to that from your server, with something like fastify-http-proxy:

const proxy = require('fastify-http-proxy');

// Normal routes here
fastify.register(proxy, { upstream: 'http://localhost:1234' });

fastify.listen(process.env.PORT, '0.0.0.0');

Upvotes: 1

Related Questions