简体   繁体   English

如何将服务器端处理添加到NodeJS React项目

[英]How to add server-side processing to a NodeJS React project

First part: given the source code of a pure NodeJS web site built on a framework (such as React), how do I tell which code runs server-side and which in the browser? 第一部分:给定一个基于框架(例如React)构建的纯NodeJS网站的源代码,我如何确定哪个代码在服务器端运行,哪个在浏览器中运行?

[This link https://reactjs.org/docs/create-a-new-react-app.html#create-react-app creates a sample of this kind.] [此链接https://reactjs.org/docs/create-a-new-react-app.html#create-react-app创建此类示例。]

Then: how do I add server-side code (1) before (2) during (3) after in-browser processing? 然后:在浏览器内处理之后,如何在(3)期间的(2)之前在(2)之前添加服务器端代码?

Specific examples 具体例子

  1. before: check a request server-side and deny if not authorised 之前:检查服务器端的请求,如果未授权则拒绝
  2. during: browser initiates lookup on server-held data, eg server local database/file system and displays the result 期间:浏览器启动对服务器保存的数据(例如服务器本地数据库/文件系统)的查找并显示结果
  3. after: server applies encryption to response using server private key. 之后:服务器使用服务器私钥将加密应用于响应。

Edit: this link https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ shows how to do (2) by running two servers and a proxy pass-through. 编辑:此链接https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/显示了如何通过运行两个服务器和一个代理传递来执行(2)。 It doesn't help with the other parts. 它对其他部分没有帮助。

Please see the following tutorial . 请参阅以下教程 The discussion around the skeleton of the app explains the basics and then the explanation around app.js is very helpful. 关于应用程序框架的讨论解释了基础知识,然后围绕app.js的解释非常有帮助。

In terms of the examples you provided, you can express authorization package on the server side to handle that. 根据您提供的示例,您可以在服务器端表达授权包以进行处理。 Once you have the server-side code set up you can handle the rest of the tasks. 一旦设置了服务器端代码,就可以处理其余任务。 Hopefully, it helps :) 希望它会有所帮助:)

The answer is (a) it all runs in the browser and (b) you can't. 答案是(a)全部运行在浏览器中,(b)您不能运行。 The link given creates a single page app (SPA), and nothing gets sent to the server unless you make it so. 给定的链接创建一个单页应用程序(SPA),除非您这样做,否则什么都不会发送到服务器。

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

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