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
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.
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.
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 :-
Mention homepage property in package.json
"homepage": "https://localhost:8080/simpleapp",
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With