简体   繁体   English

使用Express服务反应路线

[英]Serving react-routes with Express

I'm using react-router-dom and load components on rotes like this: 我正在像这样的死记硬背上使用react-router-dom和load组件:

    <Switch>
        <Route exact path="/home" component={Home} />
        <Route path="/services" component={Services} />
        ...and so on...
        <Route component={PageNotFound} />
    </Switch>

I build this project with webpack and have only 1 single file bundle.js. 我使用webpack构建该项目,并且只有1个单个文件bundle.js。 and use It as static files in my server.js 并在我的server.js中将其用作静态文件

import express from 'express'
const app = express()
app.use('/', express.static('./public'))
app.listen(3000)

Eveything works fine until I start trying to do something on some of my routes: 在我开始尝试在某些路线上进行某些操作之前,一切工作都很好:

app.get('/services', () => {
  console.log('services')
})

When I'm on route '/services' it doesn't log 'services' in console. 当我在路线“ /服务”上时,它不会在控制台中记录“服务”。

You need to understand the difference between the browser rendering the route and Express rendering a path. 您需要了解浏览器呈现路线和Express呈现路径之间的区别。 They are two completely different things. 它们是完全不同的两件事。 When you refresh the browser, Express does not know how to handle "/services", therefore you get an error. 刷新浏览器时,Express不知道如何处理“ /服务”,因此会出现错误。 When you type "/api/services" then express does know how to handle the path. 当您键入“ / api / services”时,express确实知道如何处理路径。 When React/React-Router is fully loaded into the browser, it does not request "/services" from the Express server, the browser already knows about the router, and renders the HTML. 当React / React-Router完全加载到浏览器中时,它不向Express服务器请求“ /服务”,浏览器已经知道路由器,并呈现HTML。

You need a wildcard route match "*" that will render the default/index.html file. 您需要一个通配符路由匹配“ *”,以呈现default / index.html文件。 This will allow your browser to locate the "/services" page. 这将使您的浏览器可以找到“ /服务”页面。 This is complicated but basically, your browser calls "/services", this hits express, who tries to find a path match. 这很复杂,但是从根本上讲,您的浏览器将其称为“ / services”,这表示要尝试找到路径匹配项。 If you have a wild card that points to your index.html page, then this is returned to the browser. 如果您有一个通配符指向index.html页面,那么它将返回到浏览器。 The Browser then looks at the route "/services" and passed this to React Router, who then load the services components. 然后,浏览器查看路由“ / services”,并将其传递给React Router,然后由后者加载服务组件。 You are always returning the same HTML (index/default), it's the browser that does the magic of showing the right page. 您总是返回相同的HTML(索引/默认值),这是浏览器神奇地显示正确页面的原因。 this is why it's important to have different paths/routes (Path: "/api/services", route: "/services". 这就是为什么拥有不同的路径/路由(路径:“ / api / services”,路由:“ / services”)很重要的原因。

If you didn't then a user refreshing the page on the "/services" route would hit express and get the "/services" path. 如果您不这样做,那么在“ / services”路由上刷新页面的用户将点击express并获得“ / services”路径。

Path: An Express pattern match for the URL to a block of code "/api/services" 路径:URL到代码“ / api / services”块的Express模式匹配

route: A ReactRouter URL for rendering a given Component route:用于呈现给定组件的ReactRouter URL

Make sure your backend route is different otherwise this gets confusing. 确保您的后端路由不同,否则会造成混淆。

app.get('/api/services', () => {
  console.log('services')
})

This way we know that: 这样我们知道:

/services = Your React-router component / services =您的React-router组件

/api/services = an API call to the backend / api / services =后端的API调用

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

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