I'm building a node + react app that uses passport's facebook authentication. Getting this authentication to work involves hitting an express route '/auth/facebook'. Unfortunately as soon as the react app loads up react router 4 doesn't allow links to directly hit the express server and instead searches for a react route matching 'auth/facebook'. In short how do I link to a route within my application but outside of the react app when using react router 4?
1 Answers
0
votes
React Router is only for client side routing. Use fetch API or a similar library for that.
I'll state one way of solving your problem (using fetch and without react router).
- Remove the href from the
<a>
tag - Add an event listener for the click event,
<a onClick={makeCall}>
Then in the makeCall function, you can call the backend using the fetch API(or axios or whatever),
makeCall() {
fetch('/auth/facebook', options)
.then((res) => {
// Something
})
.catch((err) => {
// handle error
});
}
react-router
is for client side routing, you can't get to a server route with relative path. you should include the full domain route to hit a server. (this will cause a refresh). – Sagiv b.g"proxy": "http://localhost:8080/"
in package.json of your client project. – xDreamCoding