Yes, React runs on the client and Express is a Node.js framework. There's a pretty good chance you're using Express if you're running any boilerplate.

Here's a pretty good walkthrough on more complete routing. https://medium.com/@patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d

In several of my applications my routes look something like this:

//router.js--and I'm positive this is from some react-express boilerplate
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

const react =  (req, res, next)=>{
  res.render('react', { 
    title: 'React Application',
    layout: false
  });
};

router.get('/app', react);
router.get('/app*', react);
module.exports = router;

//app.js
...
app.use('/', routes); //<--this is the exported router. 
...

If you want to be more simple it is probably something like:

   let reactRoute = (request, response, next) => {
     //render your react page however you're doing that. 
   }
   express.use('/API', yourApiFunction)
   express.use('/', reactRoute)
   express.use('/*', reactRoute) //Wildcards are REALLY important if you're routing inside react.

You can also bypass things with a proxy but that tends to get more complex than you probably want. Also--keep in mind you don't have to stick to Node on the back-end if you're not comfortable with it. React is client side, I use it with a few production .NET apps, some PHP (lordy!), and, yes, a lot of Node servers.

Answer from Daniel B. Chapman on Stack Overflow
Top answer
1 of 2
3

Yes, React runs on the client and Express is a Node.js framework. There's a pretty good chance you're using Express if you're running any boilerplate.

Here's a pretty good walkthrough on more complete routing. https://medium.com/@patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d

In several of my applications my routes look something like this:

//router.js--and I'm positive this is from some react-express boilerplate
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

const react =  (req, res, next)=>{
  res.render('react', { 
    title: 'React Application',
    layout: false
  });
};

router.get('/app', react);
router.get('/app*', react);
module.exports = router;

//app.js
...
app.use('/', routes); //<--this is the exported router. 
...

If you want to be more simple it is probably something like:

   let reactRoute = (request, response, next) => {
     //render your react page however you're doing that. 
   }
   express.use('/API', yourApiFunction)
   express.use('/', reactRoute)
   express.use('/*', reactRoute) //Wildcards are REALLY important if you're routing inside react.

You can also bypass things with a proxy but that tends to get more complex than you probably want. Also--keep in mind you don't have to stick to Node on the back-end if you're not comfortable with it. React is client side, I use it with a few production .NET apps, some PHP (lordy!), and, yes, a lot of Node servers.

2 of 2
-1

to solve No 'Access-Control-Allow-Origin' header is present on the requested resource. you've to use the cors middleware

go to the term yarn add cors or npm i cors

in server.js

const cors = require("cors");
const express = require("express");
const app = express();

app.use(cors());
🌐
DEV Community
dev.to › codingjlu › developing-express-and-react-on-the-same-server-55p7
Developing Express and React on the same port - DEV Community
January 17, 2022 - Experiment and change Bob to something else. When you're comfortable with Express (which you probably already are) you can go ahead and change the server however you like. You can create new files, edit the file, perform backend operations, and more! We can also customize the client-side (React).
🌐
LogRocket
blog.logrocket.com › home › running react and express with concurrently
Running React and Express with concurrently - LogRocket Blog
June 4, 2024 - When you run the npm start command, concurrently will start the React and Express servers, allowing them to work together seamlessly. Additionally, concurrently provides options to customize behavior, such as controlling the number of processes ...
🌐
Vikramatech
vikramatech.co › article › setup-react-and-express-on-same-server
Setup react and express on same server
We are not creating a full fledged app in this article. We will just create a very basic frontend application in React js with two buttons for get and post requests to our Node js backend server so you can understand how to setup both applications on same server and further modify apps for additional features.
🌐
Reddit
reddit.com › r/webdev › how can i host express backend and react frontend on the same port?
r/webdev on Reddit: How can I host express backend and react frontend on the same port?
July 20, 2022 -

So, suppose I'm making a website which has a contact page which contains a form taking firstname,lastname and a message and sending it to server on the port "localhost:3000/contact.

I can use axios to make a post request and handle the request on the backend.

But when I try to host my sever on the same port(3000) I get an error "cannot get /" ,

Tried serving the index.html inside react's public folder but it's still not working.

I tried serving it on the different port(5000) and it works but isn't it similar to sending the data to a different url or website?

I want to make a post request to localhost:3000/contact and handle it on the server side(so I can save it in mongoDB) without having to host server and client separately.

🌐
freeCodeCamp
forum.freecodecamp.org › t › how-to-deploy-react-express-at-same-server › 403707
How to deploy React + Express at same server? - The freeCodeCamp Forum
June 15, 2020 - I’m trying to Deploy my React + Express at the same time. In Dev mode, without Cors, i could develop with below proxy configure //package.json //React(3000) => Express(8080) "proxy": "http://localhost:8080", But to deploy production, when i used npm run build and serve -s build, React is ...
Find elsewhere
🌐
Quora
quora.com › How-do-I-run-React-and-Express-on-the-same-port
How to run React and Express on the same port - Quora
Answer (1 of 3): You don’t. You either a React app or an Express app. However, you can use React in an Express app. Here are a couple of useful links as to how you could achieve this: * Create React App · Set up a modern web app by running one command. * Add React to a Website – React ...
🌐
Medium
geoffroymounier.medium.com › host-a-react-app-and-its-express-server-on-the-same-ec2-vps-instance-bf272f376141
Host a React app and its Express server on the same EC2/VPS instance | by Geoffroy Mounier | Medium
August 19, 2020 - So basically, you have a /dist folder where we host the webpack output bundle bundle.js. This bundle has to be accessible by both the React app and the Express server. We have an /src folder where we put both our React app and our server, respectively in /app and /server.
🌐
The Valley of Code
thevalleyofcode.com › how-to-serve-react-from-same-origin
How to connect your React app to a backend on the same origin
In production, you are going to run npm run build when you are ready to deploy and we will use the Express server to serve those static files. The whole proxy thing is now useless (and will not work in production, too - it’s meant to ease development). Which means you can leave it in the package.json file if you find it convenient. In the code below, we require the path built-in Node module and we tell the app to serve the static build of the React app:
🌐
GitConnected
levelup.gitconnected.com › how-to-render-react-app-using-express-server-in-node-js-a428ec4dfe2b
How to Render a React App Using an Express Server in Node.js | by Yogesh Chavan | Level Up Coding
November 12, 2024 - In this article, we will explore how to render a React app using Express.js server. This is very useful if you are creating a full stack app using Express.js, React.js, Node.js and any database in Nodejs. So we can run our React.js and Node.js app on the same port, avoiding the need of running two separate commands to start Node.js and React.js app.
🌐
GitHub
github.com › facebook › create-react-app › discussions › 12855
react app and express backend server in the same project · facebook/create-react-app · Discussion #12855
My question is more about maintaining react and express in a single git repository, aligning the npm commands to run both front end and backend. Beta Was this translation helpful? Give feedback. ... Something went wrong. ... Beta Was this translation helpful? Give feedback. ... Something went wrong. ... /my-react-express-app |-- /client (React app) | |-- ... | |-- /server (Express app) | |-- ... | |-- package.json (Main project's package.json) Beta Was this translation helpful? Give feedback. ... Sign up for free to join this conversation on GitHub.
Author   facebook
🌐
Reddit
reddit.com › r/reactjs › how do i use react with an express application?
r/reactjs on Reddit: how do I use react with an express application?
September 6, 2023 -

hi so basically I have been using express generator to make new express apps. I can make servers and then endpoint routes and then I then render html files for each of the routes visited and i use PUG as the view engine.

app.set("views", path.join(__dirname, "../views"));app.set("view engine", "pug");

Pug is nice but I am trying to figure out how to use react instead of making views for every route. Also as far as styling, using express generator I seem to only be able to edit styles.css to write vanilla css and then I have to serve the static path.

app.use(express.static(path.join(__dirname, "../public")));

In a nutshell, I am just trying to figure out how to use react with nodejs and express. I read a couple of guides so I think I have the directory structure correct (client and server, each with their own node modules), but not sure on how to proceed. any advice? I've attached my repo. thanks.

https://github.com/ForkEyeee/blog-api

Top answer
1 of 6
43
The above comments didn't mention the core of this post. OP is coming from the templating world (PUG, etc). This is essentially, technically, traditionally server-side rendering. The server handles the request, determines what to render, then sends out a bunch of HTML to the browser. Since 2010s, client side frameworks (like AngluarJS, React, Vue) emerged. In simple words they use pure Javascript to generate the DOM (more or less equivalent to HTML in JS world). The HTML (index.html) is just a blank shell pointing to a script to render UI. This is called Client Side Rendering (CSR). JS can also handle client side routing by mimicking URL changes but under the hood it's just showing and hiding stuff with JS. This is Single Page App (SPA). These files can be served statically with any server/service (like nginx, caddy, S3), and probably not ExpressJS most of the time. So, the first answer to this post is: No you don't need to use Express if you just want a simple SPA, follow the Vite Tutorial till "Deploying" ( https://vitejs.dev/guide/static-deploy.html ). Then, your app probably needs data. The browser has a function called fetch() to fetch data from URL. If you fetch https://www.google.com you will get some HTML strings because it is serving a website directly. If you fetch https://reqres.in/api/users you will get a JSON containing some JSON data. When you build an SPA, all dynamic data will be coming from some API. Express usually works with any SPA as a REST api server, rather than serving the HTML directly. But, SPA is not Search engine friendly because the index.html does not contain any data at all (IDK, thats what people said 5 years ago :/). People started to want SSR again, but still wanted to use React. We then have https://react.dev/reference/react-dom/server . It is used by frameworks like Next.js to render complete HTML with the react rendering, on the server, send it to browser, then "hydrating" the website to be a complete react app. OK, I have gone too far. Anyway, it's still react and your data can still come from a separate Express server no matter what framework you're using. Someone said "Fetching from server A to server B then render HTML to browser is too slow". Then Next.JS and the react team now advocates "React Server Components" (RSC), (not to be confused as SSR) (not to be discussed here). They encourage putting backend logic directly in your Next.js app without an extra API server. So the Express server is not necessary too. As long as you chose react, the express server will never be a templating engine anymore. It's a REST API server that provides data for your react app.
2 of 6
6
Your express is an API, it’s sole purpose would be to serve and ingest content Your client (react app) should make calls to the API then render the data appropriately
🌐
DEV Community
dev.to › loujaybee › using-create-react-app-with-express
Using Create-React-App with Express - DEV Community
January 11, 2020 - When running npm run start your app will proxy from port 3000 to port 8080 automatically. However when you create a production build your entire app should be running from port 8080, as express and the API's are running from the same server (in this ...
🌐
freeCodeCamp
freecodecamp.org › news › create-a-react-frontend-a-node-express-backend-and-connect-them-together-c5798926047c
How to create a React frontend and a Node/Express backend and connect them
September 7, 2018 - Inside the API directory, go to bin/www and change the port number on line 15 from 3000 to 9000. We will be running both apps at the same time later on, so doing this will avoid issues.
🌐
YouTube
youtube.com › devistry
Serve a React app from an Express server | React frontend and Express API setup in 1 project! - YouTube
React and Express are the most popular web framework stack. Many developers create their React and Express projects separately. This means that you have to h...
Published   October 14, 2022
Views   16K
🌐
Medium
medium.com › swlh › building-a-react-app-with-a-express-back-end-in-the-same-project-with-external-access-to-a-mysql-e06ef83c257d
Building a React app with a Express back-end in the same project with external access to a MySQL database | by Carlos Cuba | The Startup | Medium
April 28, 2020 - What you need to know: Basic knowledge of ES6, latest React code practices, and NodeJS for the Express server. Let’s get started! I. Creating and Proxying · Open a terminal of your choice and navigate to a folder where you want to place your project, then run the command: npx create-react-app react-with-sql (where react-with-sql is the project name used for this article). Once done, you’ll have a very basic project made with CRA.