1
votes

I have an Express/React application that uses React Router, and I'm not sure how to handle unspecified routes from the React end of things.

My express configuration calls app.get('/*', handler), which renders index.html so that the routing responsibilities are passed off to React. How can I use React Router to handle routes that haven't been predetermined. For example, if I got to myapp.com/test, I get the following error:

Location "/test" did not match any routes

How can I configure react-router to serve a 404 page, or to redirect to the default route if the URL is unrecognized?

1

1 Answers

1
votes

React Router has a similar directive:

<Route path="*" component={NotFound} status={404} />

This will render component NotFound and ensure 404 status is sent to the client in case you need it for server side rendering - universal/isomorphic apps.

Hope that helps