简体   繁体   English

如何使用Express和React作为UI构建器在后端处理路由

[英]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和纯htmlcss ,方法很简单,我可以完美地处理路由。 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.

相关问题 为什么我的前端(react)和后端(express)没有连接 - why my front end(react) and back-end(express) is not connecting 如何使用后端 Node.js Express-Sessions 对 React 前端进行身份验证? - How to authentication React Front End with back-end Node.js Express-Sessions? 如何同时运行我的前端反应和后端反应? - How do I run my react front-end and express back-end together? 如何在 react 前端使用 fetch() 从 express 后端获取数据? - How to use fetch() in react front-end to get data from express back-end? 单击按钮如何将数据从React前端发送到Nodejs Express后端 - How to send data from React front-end to Nodejs Express back-end on button click 前端(反应)与后端(快速)如何相互作用? - How does front-end (React) interact with back-end (Express)? 使用 Express JS 后端反应原生应用程序定制? - React Native App Customization using Express JS Back-end? 如何将图像尺寸从 javascript 发送到后端(快递) - How to send image dimension from javascript to back-end (express) (设计问题)如何分离前端和后端以保护路由(后端)代码? (Node.js-快速-反应) - (Design question) How to decouple front- and back-end to protect routing (backend) code? (Node.js - Express - React) 验证Nodejs / Express后端中的输入并将结果发送到React前端 - Validate input in Nodejs/Express back-end and send result to React front-end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM