React app returning 500 Internal Server Error
Solution 1
When you’re visiting a route of your app directly, e.g. via https://myapp.com/route/123 Apache tries to map that URL to a file in the public folder. In this case it looks for /route/123.html which obviously doesn’t exist. To avoid that, we have to tell Apache to redirect all requests to our index.html so our app can perform some routing magic for that URL. To tell Apache to redirect requests to index.html where our app lives, we have to modify the .htaccess file. If there is no such file in your app’s folder yet, just create it.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
for more information check this out.
Solution 2
Your server's route should match the routes defined in the react-router, If they don't match then you will receive a 500 error.
Solution 3
Can you share some error log screenshot or copy-paste ? 500 is a server-side error, obviously. Maybe your routes on server are not matching the url pattern. Or some express server route function threw an exception.
Please, provide some logs from client and\or server.
UPD: Just mentioned the "static site" thing. Didn't understand what exactly do you mean by that. For me it's no server at all.
Still i'm pretty sure that your server has no routes configured.
Server knows what is "/"("index.html"). But there are no routes configured for, say, "/potatoes".
In express server you would do something like:
app.get('*', function(req, res){
res.sendFile(__dirname + '/index.html');
});
- meaning, ALL GET REQUESTS to your server(app) will lead the user to same "index.html".
Ally Jr
Updated on July 09, 2022Comments
-
Ally Jr almost 2 years
I have a react app, created using create-react-app.
After I run
npm run build
, and deploy the app as a static site, everything works except when I refresh the page on a route other than the index, it fails with a500 internal server error
.Below is the code for my router. Using react-router.
<Router history={browserHistory}> <Route path="/"> <IndexRoute component={Login}/> <Route path="/dashboard" component={Dashboard}></Route> <Route path="/profile" component={Profile}/> </Router>
Any help appreciated.
-
Ally Jr about 7 yearsHi i will thanks. But the app is built and rendered as a single html page with js and css assets. the routing should all be client side...?
-
Alex Frolov about 7 yearsFor "create-react-app" i guess yeah, should be only client-side. But again, 500 is a server-side error, which means you do use some kind of server to run that app. 'webpack-dev-server' i guess ))) Anyways you should get some messages in CMD/console where you run "npm run start" or something like that to run your app.
-
Saurabh Bhandari about 7 yearsWelcome to Stack Overflow ! Please read How to Ask Question and provide a Minimal, Complete, and Verifiable example !
-
Alex Frolov about 7 yearsAnd what was that "minus" for exactly ?
-
gordie about 3 yearsdoes this support URL parameters ? Because I got 404 errors now, but I'm not sure this is related. Tnx !
-
Mehdi Hosseini about 3 yearsYes, It does support URL parameters.