This is the most basic question.
In every online example I see, to get a website running with NodeJS and React two seperate programs/servers are created. One runs on port 3000 and the other on 3001. Is this necessary? If react is a frontend engine why does it have to run on a port or a server?
Im confused as to why they always use create-react-app which may be running on its own server im not sure.
I would like to create app.js import 'express' to handle requests and also import React on the same file, but I run into compilation errors, will this approach work?
CodePudding user response:
Is this necessary?
No, this isn't ever necessary and is only typically done in development.
If react is a frontend engine why does it have to run on a port or a server?
Webpack (the thing typically bundling your React code for browser usage, similar tools are vite/snowpack) creates a server for live updates and errors so changes in your React automatically propagate to the UI.
This is only done during development and not in production. It's a convenience feature while developing the code.
I would like to create app.js import 'express' to handle requests and also import React on the same file, but I run into compilation errors, will this approach work?
Frontend code usually has a build step where you run npm run build
which runs webpack --production
over your code (or esbuild or other similar tools). After this point all your frontend code is typically static files (.js .html .css etc) from which point you just serve them from static file storage (usually a CDN for faster load times in different geographies).
CodePudding user response:
Working with React and Express would work, in fact, it's a very popular tech stack.