简体   繁体   English

Express.js/Node.js Web 应用程序如何路由?

[英]How routing in Express.js/Node.js web app?

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).我真的会知道在后端(Node.js)或前端(React.js)或两者(我在互联网上不明白)处理路由器是否更好。 For example, how have I to work with the login page (that will re-direct on the user area).例如,我如何使用登录页面(将重定向到用户区域)。 Thanks谢谢

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.我不知道你的项目有多大,或者它的要求,但如果这是一个个人项目,我建议使用 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.您可以从 Front 调用您的 Express 应用程序,然后根据您得到的响应,您可以将用户路由到任何地方。

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.我会在前端监听响应,如果收到成功消息,我会从 React Router 调用 useNavigate 将用户路由到用户页面。

Here is the documentation for React Router Dom: https://www.npmjs.com/package/react-router-dom这是 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.如果这是一个更大的项目,并且您需要考虑可访问性和搜索引擎优化,则必须找到一种从后端渲染 React 的方法。 Its called server side rendering, which I'm not familiar with.它称为服务器端渲染,我不熟悉。

Hope this helps a little.希望这有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM