Home > Back-end >  How routing in Express.js/Node.js web app?
How routing in Express.js/Node.js web app?

Time:07-18

I'm working on this web app that include several pages. I really would know if it's better handle the router in the backend (Node.js) or in the frontend (React.js) or with both (I didn't understand in the Internet). For example, how have I to work with the login page (that will re-direct on the user area). Thanks

CodePudding user response:

I don't know how big your project is, or its requirements, but if this is a personal project I would suggest using the React Router Library. You can make a call to your Express app from the Front, and based on the response you get back you can route the user to wherever.

In a project of mine I had an express route for login that looked for the user in a database, then checked to see if they provided the correct password. If the user provided the wrong password or if the account wasn't found I sent back an error message. If the user provided the right password I sent a success message to the front. I would listen for the response on the frontend, if I received a success message, I would call useNavigate from React Router to route the user to the user page.

Here is the documentation for React Router Dom: https://www.npmjs.com/package/react-router-dom

If this is a bigger project and you need to think about accessibility and search engine optimization, you would have to find a way to render React from the backend. Its called server side rendering, which I'm not familiar with.

Hope this helps a little.

  • Related