简体   繁体   English

了解ReactJS和NodeJS应用程序如何工作

[英]Understanding how ReactJS and NodeJS app can work

I'm trying to learn React JS, but I have difficulties understanding how it should work with NodeJS, let's say with Express. 我正在努力学习React JS,但是我很难理解它应该如何与NodeJS一起使用,让我们说使用Express。

So far I understand that ReactJS is "V" in "MVC" This is easy, I can already write a code using create-react-app 到目前为止,我理解ReactJS在“MVC”中是“V”这很容易,我已经可以使用create-react-app编写代码

This is how I understand that: 这就是我的理解:

  1. "natural" way of combining React and Express is to simply write frontend in React and api in Express (with Mongo for example). 将React和Express结合起来的“自然”方式就是简单地在React中编写前端,在Express中编写api(例如使用Mongo)。 This way we can simply make ajax calls from React to our /api and show data 这样我们就可以简单地从React调用ajax调用我们的/ api并显示数据

  2. there is also a possibility to use React to server side rendering, which requires little more configuration. 还有可能将React用于服务器端渲染,这需要更多的配置。 This way we do not call /api from React, we just use React to write code which can be rendered by Express 这样我们就不会从React调用/ api,我们只使用React来编写可由Express呈现的代码

My question is Am I thinking right ? 我的问题是我在想吗? Not sure about all that... Is Isomorphic JavaScript somehow related to #2 ? 不确定所有这些...... Isomorphic JavaScript是否与#2有某种关系?

You are mostly correct, but using server side rendering does not mean you do not also issue api requests ever. 你大多是正确的,但使用服务器端渲染并不意味着你也不会发出api请求。 Server side rendering is a technique used to improve initial load time. 服务器端呈现是一种用于改善初始加载时间的技术。 Rather than pulling down your javascript bundle, which then makes some api calls to load a bunch of data it needs to render, you instead do that initial rendering and bootstrapping of the app on the server. 而不是拉下你的javascript包,然后进行一些api调用来加载它需要渲染的一堆数据,而不是在服务器上进行应用程序的初始渲染和引导。 The resulting html and initial state of the application are then returned to the client so the app can be shown immediately to the user. 然后将生成的html和应用程序的初始状态返回给客户端,以便可以立即向用户显示应用程序。 So when talking about server side rendering with react, it's really about the initial load. 因此,当谈论使用react的服务器端渲染时,它实际上是关于初始负载。

After the initoal load, you still have a dynamic frontend applicatoon. 在初始加载之后,您仍然有一个动态前端应用程序。 You still end up making api requests as the user interacts woth the app. 当用户与应用程序进行交互时,您仍然会发出api请求。 If I go to a different route in the app (assuming it's a single page app) which requires additional data, I'll still be issuing a GET request to load that data. 如果我在应用程序中进入不同的路径(假设它是单页应用程序)需要额外的数据,我仍然会发出加载该数据的GET请求。 If I click a button to update a resource, I'll still be issuing a PUT or PATCH request to do so. 如果我单击按钮更新资源,我仍然会发出PUT或PATCH请求。

So in terms of the question of how express and react fit together, express (or whatever backend language/framework you use) provides the api to interact with data in your data store. 因此,就快速和反应如何融合的问题而言,表达(或您使用的任何后端语言/框架)提供api与数据存储中的数据进行交互。 And react is allowing you to build views that consume those apis. 并且反应允许您构建使用这些api的视图。 Server side rendering is just an additional technique you can use, not a totally separate paradigm. 服务器端渲染只是您可以使用的另一种技术,而不是完全独立的范例。

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

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