Home > Blockchain >  Is Create-React-App a Server Of It's Own?
Is Create-React-App a Server Of It's Own?

Time:07-13

I am pretty experienced with NodeJs and Express, but I'm a beginner in ReactJS.

I usually use ejs and deploy my server on Express like this and route server-side.

app.get("/", (req, res) => res.render("index.ejs");
app.get("/contact", (req, res) => res.render("contact.ejs", { data: data });

Something like this.

However, working with React (create react app), I find myself not using express at all for routing and only serve an API.

Create React App apparently already has a server (localhost:3000 by default) and I can perform client side routing directly there.

Should I deploy my server on a client-side framework, React?

I don't even need to serve static files on express.

Hope you understood me, thank you for your help :)

CodePudding user response:

First of all react is a library and second of all, when you say that it has a server that means it's a development server, for reviewing the results while you're working on it locally (means when you are building your application, you need to see how it looks in real-time). It's not for production use or included in the build output. Now let's say your application is ready and you want to deploy it, then you run a build command which does the following for you

  • transpiles JS code
  • bundles code and assets
  • uses cache busting techniques for assets
  • removes dead code

and you will deploy your application on a server, so that it can be accessible to clients

I don't need to serve static files on express, explanation below

In modern-day websites, we don't have to use views because react comes into the picture and takes care of the clientside-UI that the user interacts with, so you are right that we don't have to use static files in express anymore but if you have a use case where you have to use it then you can use it, no one is stopping your from doing that

and lastly you can have your own node js server as well which your front end will be talking too, not for views but for data that is stored in database that you want to fetch and display it to the user

CodePudding user response:

React by itself just create static html-css-js files, but for testing purposes, they have also included this handy way (localhost:3000 pipeline) to view the built files. Technically you don't need to use it, just use the build script, then set up an express server to serve the files from "build" folder, and it does the same thing.

About client side routing, basically what happens is the entire page gets downloaded client-side, and it uses the value in url box to show or switch to certain screens. This is why its called "Single Page Application", you are serving a single page to the client upfront with all the static code. Compare this to express-ejs where you serve static code conditionally, depending on the address.

Take a look at CRA's docs for more info about deployment.

  • Related