How to deploy react app with express js (SSR)

​​There are 2 ways you can deploy a react app on a production server.

we focus on server-side rendering with express js backend

1. Using static server

Create a build of react app and serve it statically using serve command. 

2. Serve page from expressjs

You don’t necessarily need a static server in order to run a Create React App project in production. It also works well when integrated into an existing server-side app.

First, you need to build your react code using:

npm run build
const express =require(‘express’);
const path =require(‘path’);
const app =express();
app.use(express.static(path.join(__dirname,’build’)));
//setting static path
app.get(‘/’,function(req, res){
res.sendFile(path.join(__dirname,’build’,’index.html’));
});​
app.listen(4000);

After getting build of your react code 

app.use(express.static(path.join(__dirname,’build’)));

serve the build folder as static files, and target the index.html inside it

app.get(‘/’,function(req, res){
res.sendFile(path.join(__dirname,’build’,’index.html’));
});

This will redirect all the incoming requests to index.html inside build directories. This is how react app can be deployed using SSR.
On the verge of digitization CollegeInit is the web application, which provides colleges and students a digital platform, to connect easily, which is developed and launched in the year 2020 by Market Pro. Ltd. The aim of this application is to provide a digital solution for every problem related to colleges and students. It is the largest platform connecting students and colleges together in Nepal.

Published by collegeinit

collegeinit is a web app connecting students and college.

Leave a comment

Design a site like this with WordPress.com
Get started