[英]How to handle routes in back-end with Express and React as UI builder
I started to learn ReactJS
for using in front-end of my website. 我开始学习
ReactJS
以便在我的网站前端使用。 I want to build a multiple page website, However I ran into a problem and I don't have any idea how this should be implemented. 我想建立一个多页网站,但是我遇到了一个问题,我不知道应该如何实现。
When I used Express
and pure html
and css
the way was straightforward and I could handle routes perfectly. 当我使用
Express
和纯html
和css
,方法很简单,我可以完美地处理路由。 to make it clear consider following code : 为了清楚起见,请考虑以下代码:
const express = require('express');
const app = express();
app.get('/home', (req, res)=>{
//more stuff...
res.send('home.html');
})
However in react mixed up with Express as back-end, I don't have any idea for handling the routes. 但是,在与Express作为后端混在一起的反应中,我对处理路由一无所知。 I followed some tutorial but almost all of them use
react-router
as a means of handling the routes. 我遵循了一些教程,但是几乎所有教程都使用
react-router
作为处理路由的方法。 I know this can be done using react-router
, but I'm looking for a way to handle them just like snippet above and something like below: 我知道可以使用
react-router
做到这一点,但我正在寻找一种处理它们的方法,就像上面的代码段和下面的内容一样:
//initializations
app.get('/home', (req, res)=>{
res.send(<Home and props/>)
})
app.get('/dashboard, (req, res)=>{
res.send(<Dashboard info=`${req.userInfo} />`)
})
I'm more comfortable in this way than handling with react-router
, since as I said, I'm going to create a multiple page website with server-side-rendering
. 与使用
react-router
处理相比,我以这种方式感到更自在,因为正如我所说,我将创建一个具有server-side-rendering
的多页网站。
How Can I implement this approach ? 如何实施这种方法? Thanks in advance.
提前致谢。
If you're using React, I'd encourage you to consider a single page app instead. 如果您使用的是React,建议您考虑使用单页应用程序。 It's what React was built for and single page apps are super dope.
这就是React的目标,单页面应用程序是超级涂料。
If you really want to have a separate page for each of your React components, each page will need to be its own html document and you can use renderToString to turn the component into a string that can be inserted into the html. 如果您确实希望每个React组件都有一个单独的页面,则每个页面都必须是自己的html文档,您可以使用renderToString将组件转换为可以插入html的字符串。
Here's a good article on how to do this thing I hope you don't do :-) https://medium.com/front-end-hacking/server-side-rendering-with-react-and-express-382591bfc77c 这是一篇有关如何执行此操作的好文章,希望您不要这样做:-) https://medium.com/front-end-hacking/server-side-rendering-with-react-and-express-382591bfc77c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.