2

I am creating web app with ReactJS frontend and Java EE + tomcat backend. I'm using React Router for routing. On localhost everything works fine, but on a deployed version I am getting errors.

  <Route exact path='/' component={LandingPage} />
  <Route path='/howitworks' component={HowItWorks} />
  <Route path='/about' component={About} />
  <Route path='/pricing' component={Pricing} />
  <Route path='/appartment/:id' render={(props) => <Appart {...props} />} />
  <Route exact path='/appartments' render={() => <Appartment />} />

In tomcat web.xml file I have added:

<error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
</error-page>

So everything works just fine except for:

<Route path='/appartment/:id' render={(props) => <Appart {...props} />} />

which WORKS on localhost. The error I am getting is HTTP 404.

Any suggestions? Thanks!

Edit: web.xml content

<web-app>
  <context-param>
    <param-name>ImagesFolder</param-name>
    <param-value>/images</param-value>
  </context-param>
  <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.web.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>  
   <welcome-file>index.html</welcome-file>    
  </welcome-file-list>  

    <error-page>
        <error-code>404</error-code>
        <location>/index.html</location>
    </error-page>
</web-app>
dm98
  • 53
  • 4
  • What is the difference of the deployed version? Does it run behind a LB or different root servlet path? – JScoobyCed May 18 '20 at 15:35
  • @JScoobyCed hey, not sure what LB means.Can you elaborate? thanks! – dm98 May 18 '20 at 15:43
  • I meant Load Balancer. Is there a network (hardware/software) element in deployed version? On localhost are you running in tomcat as well? What is your web.xml descriptor – JScoobyCed May 18 '20 at 15:49
  • @JScoobyCed yes on localhost I am also running in tomcat. And yes root servlet path is different on deployed version, but all queries and routes work except for the one with params so I am guessing there is the problem. Do you want me to paste the contents of web.xml? – dm98 May 18 '20 at 16:40
  • Yes, if there is no private info in web.xml, please share. Can you also explain the differences of localhost vs prod. I.e. the servlet path is different due to config only in web.xml, or any proxy/system. Do you connect directly to prod tomcat or is there any network hardware/software in between? Is the 404 returned by tomcat or something else? – JScoobyCed May 20 '20 at 14:26
  • @JScoobyCed edited my initial question with web.xml contents. Prod version is deployed at hosting provider's tomcat server. The only actual difference is in ReactJS's file where I proxy requests to hosting provider's server (there is no other software / hardware). Maybe it's best if you check out the site yourself. Check console. Thanks! http://go-town.com/appartment/1 – dm98 May 20 '20 at 16:31
  • I notice this: When we hit URLs, which are working, favicon is loaded successfully through `http://go-town.com/favicon.ico` (I chose favicon for simplicity). But when we hit `/apartment/1`, it tries to load favicon from `http://go-town.com/appartment/favicon.ico` (which results in 404). I am still trying to understand this. – Ajeet Shah May 20 '20 at 16:46
  • 2
    It looks like every page returns a 404 AND reactjs is still picking it up. Are you using ro from the react-router package? – JScoobyCed May 24 '20 at 05:26
  • @JScoobyCed I was using div with onClick event handler in which I was setting the value of window.loaction object. I chaned it to Link with to attribute and now it works as it should. I still have two questions: Why is it not working with setting the window.location and why was it working on localhost at all. Anyway thank you for your help!!!! – dm98 May 24 '20 at 13:01
  • Using `Link` from react router takes you to the route without refreshing your page unlike `window.location.replace` which refreshes whole app to go to that route. – Ajeet Shah May 24 '20 at 13:11
  • in your index.html add this tag after meta – Enrique Flores Feb 05 '21 at 19:34

0 Answers0