简体   繁体   English

连接前端和后端 MERN 堆栈

[英]Connecting frontend and backend MERN stack

How does the react client connect to the server via express? react客户端是如何通过express连接到服务器的? Many tutorials talk about Superagent and axios which is adding to my confusion.许多教程都在谈论 Superagent 和 axios,这增加了我的困惑。 Are there any resources on server side routing in the context of react?在反应的上下文中,服务器端路由是否有任何资源? thank you谢谢

In MERN stack, you do not necessarily have to think of the entire stack as a single entity.在 MERN 堆栈中,您不必将整个堆栈视为单个实体。 Mongo, ReactJS and NodeJS server can all work independently. Mongo、ReactJS 和 NodeJS 服务器都可以独立工作。 And let us for easiness of understanding sake say all of them are on separate servers.为了便于理解,让我们说所有这些都在不同的服务器上。 That is we can have Mongo on one server, ReactJS on another server and NodeJS with express on a third server, then also it will be a MERN stack app.也就是说,我们可以在一台服务器上使用 Mongo,在另一台服务器上使用 ReactJS,在第三台服务器上使用 Express 的 NodeJS,那么它也将是一个 MERN 堆栈应用程序。

How a MERN app work is as follows MERN 应用程序的工作原理如下

For example, let us have an app that displays the details of all the students in a class.例如,让我们有一个应用程序来显示班级中所有学生的详细信息。 First, in the React app let us say you select a class, and then the React front-end will send a query to the nodejs server.首先,在 React 应用程序中,假设您选择了一个类,然后 React 前端将向 nodejs 服务器发送查询。 The query will contain the particular class name.查询将包含特定的类名。 Now nodejs will send a query to the mongo db asking for the details of the students of that class which it will send back to the node server.现在 nodejs 将向 mongo db 发送一个查询,询问该班级学生的详细信息,它将发送回节点服务器。 The node server will then send the details to the front end and it will update it.节点服务器然后将详细信息发送到前端并更新它。

If you ask for connection as such, there can be no connection at all except for querying for data.如果您这样要求连接,除了查询数据外,根本没有连接。 Instead of using the reactjs front end you can use some other frontend and it will give you the same details.您可以使用其他前端,而不是使用 reactjs 前端,它会为您提供相同的细节。 React, Mongo and Node, all are capable of working on their own in their respective fields. React、Mongo 和 Node,都能够在各自的领域独立工作。

Axios is a promise based HTTP client for the browser and node.js. Axios 是用于浏览器和 node.js 的基于 Promise 的 HTTP 客户端。

They are completely independent.他们是完全独立的。 Whether using axios, the native Javascript fetch, jQuery AJAX, etc...each of them runs in the browser and makes a GET/POST request to nodejs.无论是使用 axios、原生 Javascript fetch、jQuery AJAX 等……它们中的每一个都在浏览器中运行,并向 nodejs 发出 GET/POST 请求。 You will have defined corresponding GET/POST routes within nodejs to respond to these requests and return JSON response data for them to consume.您将在 nodejs 中定义相应的 GET/POST 路由来响应这些请求并返回 JSON 响应数据供它们使用。

I would start by forgetting about react altogether.我首先会完全忘记反应。 Instead build an express API with various GET/POST routes that return JSON responses.而是使用返回 JSON 响应的各种 GET/POST 路由构建一个快速 API。 Test with a simple client like postman .使用像postman这样的简单客户端进行测试。 Once you have a handle on that, then start with a front-end Javascript framework to consume these services.一旦你掌握了这个,然后开始使用前端 Javascript 框架来使用这些服务。

Here is a cut of my express+react api:这是我的 express+react api 的一部分:

var express = require('express');
var router = express.Router();

router.get('/', function (req, res) {
    res.render('index', {myjson: "myValue"});
})

module.exports = router;

Basically I am sending the json string to index.jsx, where the frontend is rendered.基本上我将 json 字符串发送到 index.jsx,在那里呈现前端。

Also I've set in express as:我还把快递设置为:

app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');
app.engine('jsx', reactViews.createEngine());

So the express server knows where React is.所以 express 服务器知道 React 在哪里。

Checkout the npm package Express-react-engine .查看 npm 包Express-react-engine

All the elements of the stack can be used independently, React , Node.Js, and MongoDB.堆栈的所有元素都可以独立使用,React、Node.Js 和 MongoDB。 They can be installed in different servers and the communication is by using Fetch, Axios or any other tool.它们可以安装在不同的服务器上,并且通过使用 Fetch、Axios 或任何其他工具进行通信。

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

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