Integrate React With Node - Js Express On Same Server:Port - BezKoder
Integrate React With Node - Js Express On Same Server:Port - BezKoder
Search…
Download Free
O'Reilly eBook
Successfully experiment
and integrate product
features in your so!ware
Integrate React with Node.js Express on same applications
Server/Port
Split So!ware
! Last modi!ed: December 4, 2020 (https://bezkoder.com/integrate-react-express-same-server-port/) "
bezkoder (https://bezkoder.com/author/bezkoder/) # Full Stack (https://bezkoder.com/category/full-
stack/), Node.js (https://bezkoder.com/category/node-js/), React (https://bezkoder.com/category/react/)
Open
In this tutorial, I will show you step by step to integrate React project with Node.js Express
Rest API so that we only need to run both on same Server/Port. You will also know how to
configure React SPA Routing to avoid 404 on refresh.
Related Posts:
– React + Node.js Express + MySQL: CRUD example (https://bezkoder.com/react-node-
express-mysql/)
– React + Node.js Express + PostgreSQL: CRUD example (https://bezkoder.com/react-
node-express-postgresql/)
– React + Node.js Express + MongoDB: CRUD example (https://bezkoder.com/react-node-
express-mongodb-mern-stack/)
– React + Node.js Express + MySQL/PostgreSQL: JWT Authentication example
(https://bezkoder.com/react-express-authentication-jwt/)
– React + Node.js Express + MongoDB: JWT Authentication example
(https://bezkoder.com/react-node-mongodb-auth/)
Serverless:
– React Firebase CRUD with Realtime Database (https://bezkoder.com/react-firebase-
crud/)
– React Firestore CRUD App example | Firebase Cloud Firestore
(https://bezkoder.com/react-firestore-crud/)
Contents [hide]
https://bezkoder.com/integrate-react-express-same-server-port/ Page 1 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
Squarespace
Open
HA Proxy GUI
HA Proxy GUI and HA Proxy
Reporting
https://bezkoder.com/integrate-react-express-same-server-port/ Page 2 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
Otherwise, when deploying React production-build with Node.js Express project, we only
need to run Node Project for the fullstack (React + Node) system.
Technology Stack
Node.js
Express
React 16
React Router
React Scripts 3
https://bezkoder.com/integrate-react-express-same-server-port/ Page 3 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
You can use your own React Project, or just download the source code on Github
(https://github.com/bezkoder/react-crud-web-api), or follow these steps to create a new
one.
After the process is done. We create additional folders and files like the following tree:
$ public
$ src
$ components
! add-tutorial.component.js
! tutorial.component.js
! tutorials-list.component.js
$ services
! tutorial.service.js
! App.css
! App.js
! index.js
! package.json
Because I want to keep this tutorial simple and brief, please continue to develop this App
with instruction in the post:
React.js CRUD example to consume Web API (https://bezkoder.com/react-crud-web-api/)
$ mkdir react-express-mysql
$ cd react-express-mysql
https://bezkoder.com/integrate-react-express-same-server-port/ Page 4 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
npm init
name: (react-express)
version: (1.0.0)
description: integrate React and Node.js Express Rest Apis on same server
entry point: (index.js) server.js
test command:
git repository:
keywords: react, nodejs, express, sequelize, rest, api
author: bezkoder
license: (ISC)
Run command:
– npm run build
– or yarn build
52.63 KB build\static\js\2.8091b331.chunk.js
22.44 KB build\static\css\2.47e06e2e.chunk.css
2.38 KB build\static\js\main.2cd554a3.chunk.js
774 B build\static\js\runtime-main.efee8004.js
144 B build\static\css\main.9c6cdb86.chunk.css
bit.ly/CRA-deploy
https://bezkoder.com/integrate-react-express-same-server-port/ Page 5 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
Now you can see new build folder with content as following:
$ app
$ config
$ controllers
$ models
$ routes
$ views
! index.js
! package.json
! server.js
Now we need to copy all files from React build folder to app/views folder above.
https://bezkoder.com/integrate-react-express-same-server-port/ Page 6 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
app.use(express.static(path));
http://localhost:8080/index.html
http://localhost:8080/static/css/2.47e06e2e.chunk.css
http://localhost:8080/static/js/2.8091b331.chunk.js
The final step is to deliver index.html file using res.sendFile() . We will need to pass in
a path to the file.
https://bezkoder.com/integrate-react-express-same-server-port/ Page 7 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
app.use(express.static(path));
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const db = require("./app/models");
db.sequelize.sync();
require("./app/routes/turorial.routes")(app);
https://bezkoder.com/integrate-react-express-same-server-port/ Page 8 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
To handle this error, we’re gonna enable hash(#) in React App Routing by using
HashRouter (https://reactrouter.com/web/api/HashRouter) .
It will keep the UI route in hash part of the URL, which should not make the server return
404.
So, in React Project, let’s open index.js file and change BrowserRouter into
HashRouter .
ReactDOM.render(
<HashRouter>
<App />
</HashRouter>,
document.getElementById("root")
);
serviceWorker.unregister();
https://bezkoder.com/integrate-react-express-same-server-port/ Page 9 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
Don’t forget to rebuild the React App and copy all files and folders in build directory (React
project) into views (Node.js Express project).
Now you can refresh the page without worrying about 404.
Conclusion
Today we’ve learned how to integrate React with Node.js Express on same Server/Port. We
also handle the case React Router 404 Not found on Refresh by adding hash(#) to the urls.
There are many React + Express examples that you can apply this approach to integrate:
– React + Node.js Express + MySQL: CRUD example (https://bezkoder.com/react-node-
express-mysql/)
– React + Node.js Express + PostgreSQL: CRUD example (https://bezkoder.com/react-
node-express-postgresql/)
– React + Node.js Express + MongoDB: CRUD example (https://bezkoder.com/react-node-
express-mongodb-mern-stack/)
– React + Node.js Express + MySQL/PostgreSQL: JWT Authentication example
(https://bezkoder.com/react-express-authentication-jwt/)
– React + Node.js Express + MongoDB: JWT Authentication example
(https://bezkoder.com/react-node-mongodb-auth/)
Further Reading
https://www.npmjs.com/package/express
(https://www.npmjs.com/package/express)
React Component (https://reactjs.org/docs/react-component.html)
https://reactrouter.com/web/api/ (https://reactrouter.com/web/api/)
Source Code
You can find the complete source code for this tutorial at Github
(https://github.com/bezkoder/react-express-mysql).
https://bezkoder.com/integrate-react-express-same-server-port/ Page 10 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
Open
Share & Get Paid Node.js, Express Build Your App React.js +
& MongoDb: without Coding Node.js +
Build a CRUD… Express +…
Ad SurveyJunkie.com bezkoder.com Ad Appy Pie bezkoder.com
Leave a Reply
Your email address will not be published. Required !elds are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
% Vue.js + Node.js + Express + MongoDB example: MEVN stack CRUD Application (https://bezkoder.com/vue-node-express-
https://bezkoder.com/integrate-react-express-same-server-port/ Page 11 of 12
Integrate React with Node.js Express on same Server/Port - BezKoder 3/5/21, 2:55 PM
mongodb-mevn-crud/)
Upload & resize multiple images in Node.js using Express, Multer, Sharp & (https://bezkoder.com/node-js-upload-resize-multiple-
images/)
FOLLOW US TOOLS
(https://www.dmca.com/Protection/Status.aspx?
" Json Formatter (https://bezkoder.com/json-formatter/) ID=3f543dd5-c6d8-4208-9a6b-
0e92057fd597&refurl=https://bezkoder.com/integrate-
(htt react-express-same-server-port/)
ps://
ww
w.yo
utub
e.co
m/c
han
! nel/ #
der) 6Q) )
https://bezkoder.com/integrate-react-express-same-server-port/ Page 12 of 12