Jenny Mok
Jenny Mok

Reputation: 2804

express cannot GET error using with react

I manage to setup react, basically express just as the api server. I can navigate around my react app using react-router, it's a single page application.

But when I refresh for example http://localhost/login I got error of cannot GET /login I know the issue, but I don't know how to fix it.

Upvotes: 0

Views: 731

Answers (1)

macbem
macbem

Reputation: 1230

You haven't posted your code, but I guess that you don't return the React app at the * route, which means that if you start at /, you'll be fine because it's handled, but if you want to get any other endpoint not via the app itself, but directly via an address, Express will look for a /login (for example) route handler and hence it's not there, it'll return 404.

TLDR: send the React SPA as the response on * GET and React Router will handle the rest.

Upvotes: 3

Related Questions