Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deploy reactJS application on Tomcat Server

I am new in reactjs, I want to deploy my reactjs application on tomcat server instead of running on react server which is default port is 3000. I run the npm serve command for running the npm local server but need to access that application using tomcat server.

Any suggestions please.

Thanks

like image 452
Pravin Abhale Avatar asked Dec 27 '17 06:12

Pravin Abhale


People also ask

Can we deploy React app on Tomcat server?

React router takes the root URL by default, hence able to serve static resources from tomcat ROOT directory. But Once you deploy the build files to a different path is not able to serve static content. In this article, we will see how we can configure our code and deploy in tomcat successfully.

What is the best way to deploy React app?

Deploying using Netlify drag-and-drop or Github connection If you want to deploy React app, you need to drag and drop the build folder onto the Netlify Dashboard. Run npm run build or yarn build before deploying the latest build. If everything was done correctly, you will need to see the next screen.


2 Answers

npm run build can trigger the build process but it generates code assuming the base url doesnt have any relative path , but what if i want to deploy to a relative path in my server ??. So what i mean by relative path is that, I have a simpleapp folder inside my tomcat webapps directory and i want to access my react app from that i.e http://localhost/simpleapp . It gets even more complex if you use react router .

I was getting a blank page but later, I found Some changes done on react router and package.json that solved my issue .

So the solutions that i found are :-

  1. Mention homepage property in package.json

    "homepage": "https://localhost:8080/simpleapp",

  2. For react router we must add basename property .

In above case for app to be deployed on simpleapp relative path

<Router basename={'/simpleapp'}>
  <Route path='/' component={Home} />
  {/* … */}
</Router>
like image 158
Passcode Avatar answered Oct 12 '22 05:10

Passcode


You need to first decide on how you are going to build your project using Grunt or webpack. You can also add scripts like npm run build in your package json which can trigger the build process. The built project can be used in any web server by just placing the built files and including the script in your index.html.

like image 38
Nikhil Koul Avatar answered Oct 12 '22 04:10

Nikhil Koul