简体   繁体   English

在没有互联网连接的 Windows 服务器上托管静态反应应用程序的最佳方法是什么

[英]What is the best way to host a static react application on a Windows Server that doesn't have internet connection

I created a React application using Create-React-App and npm.我使用 Create-React-App 和 npm 创建了一个 React 应用程序。 I intended to simply build it with npm run build, and somehow host the produced build in a windows server.我打算简单地使用 npm run build 构建它,并以某种方式在 Windows 服务器中托管生成的构建。

This windows server doesn't have any internet connection (but I have admin right so I can install offline packages), then I tried :这个 Windows 服务器没有任何互联网连接(但我有管理员权限,所以我可以安装离线包),然后我尝试:

  • Hosting the built folder wihth IIS which didn't work (I couldn't find any good documentation on that)用 IIS 托管构建的文件夹,但它不起作用(我找不到任何好的文档)
  • Packing the "serve" npm package to install it in the server (serve having a huge amount of dependencies, it's really difficult to get an offline package)打包“serve”npm 包安装到服务器中(serve 有大量的依赖,离线包真的很难搞定)

None of this seems to work... Any suggestion ?这些似乎都不起作用......有什么建议吗?

One way of doing it:一种方法:
1.Install NodeJS on Win Server that has Internet connection. 1.在有 Internet 连接的 Win Server 上安装 NodeJS。
2. Install Express on that server and get your React app to work with Express. 2. 在该服务器上安装 Express 并使您的 React 应用程序与 Express 一起使用。 You might want to look for Express-React boilerplate to save time.您可能想要寻找 Express-React 样板以节省时间。 I wrote such a boilerplate though it's written in TypeScript.我写了这样一个样板,虽然它是用 TypeScript 编写的。 There are others.还有其他人。
3. Copy NodeJS installer and Express installation to the server without Internet connection. 3. 将NodeJS installer 和Express 安装复制到没有Internet 连接的服务器上。 Install Node.安装节点。 Then you can optionally use IIS as a proxy for better security.然后,您可以选择使用IIS 作为代理以提高安全性。

Another (and more involved) way:另一种(更复杂的)方式:
Instead of copying the Express installation, create a Docker container out of it.不要复制 Express 安装,而是从中创建一个 Docker 容器。 Then enable Hyper-V on the server without Internet connection and run the container there.然后在没有 Internet 连接的服务器上启用 Hyper-V 并在那里运行容器。

Yet another way (I don't recommend it):另一种方式(我不推荐):
Let IIS create NodeJS run-time environment while serving incoming request using iisnode as described here .让IIS创建的NodeJS运行时环境,同时利用服务传入请求iisnode描述这里

PS聚苯乙烯
Using Express won't add SSR to your React app.使用 Express 不会将 SSR 添加到您的 React 应用程序。 An SPA needs some server-side logic from the server. SPA 需要来自服务器的一些服务器端逻辑。 That's what I meant when suggested get your React app to work with Express .这就是我建议让您的 React 应用程序与 Express 一起使用时的意思 The logic includes mapping and redirects.逻辑包括映射和重定向。

IIS has very powerful and flexible configuration and both mapping and redirects could likely be done declaratively by typing the configuration settings including regex(es). IIS 具有非常强大和灵活的配置,映射和重定向都可以通过键入包括正则表达式在内的配置设置以声明方式完成。 I never tried but have feeling this is possible.我从未尝试过,但感觉这是可能的。 However it might be a challenge to find instructions about that online.然而,在网上找到相关说明可能是一个挑战。 The other way to do the mapping and redirects is programmatically rather than declaratively.进行映射和重定向的另一种方法是以编程方式而不是声明方式。 Instead of writing it in C# for example, you can do it in JS or TypeScript, utilize Express and add IIS as a proxy.例如,您可以使用 JS 或 TypeScript,利用 Express 并添加 IIS 作为代理,而不是用 C# 编写它。

What mapping and redirects are needed?需要什么映射和重定向?

Mapping: Map the path /static/js/*.js to the physical folder where the script bundles are.映射:将路径/static/js/*.js映射到脚本包所在的物理文件夹。 CRA puts the bundles into my-cra-app/build/static/js directory. CRA 将捆绑包放入my-cra-app/build/static/js目录。 Note 1: You might want to double check that in the index.html file generated by CRA the path to script bundles (found at the bottom of the file) is indeed /static/js/<bundle>.js .注意 1:您可能需要仔细检查在 CRA 生成的index.html文件中,脚本包的路径(位于文件底部)确实是/static/js/<bundle>.js Note 2: Better security would be achieved if the mapping is not wildcarded, rather more restricted eg it checks for a pattern match that includes the bundle name(s).注 2:如果映射不是通配符,而是更受限制,例如它检查包含包名称的模式匹配,则会实现更好的安全性。

Redirects: Needed to implement the fallback required by all SPAs.重定向:需要实现所有 SPA 所需的回退。 The fallback is automatically added by CRA to webpack-dev-server.回退由 CRA 自动添加到 webpack-dev-server。 When people do deployment then in cases when they move from webpack-dev-server to Express or other server, they frequently skip the fallback which is mentioned here as the 3rd pitfall of React-Express integration.当人们进行部署时,如果他们从 webpack-dev-server 迁移到 Express 或其他服务器,他们经常会跳过回退, 这里提到了 React-Express 集成的第三个陷阱。

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

相关问题 将 React 生成的 static (SEO) “公共”前端与 CRA “私有”应用程序一起使用的最佳方式是什么? - What is the best way to have a React-generated static (SEO) “public” frontend alongside a CRA “private” app? 在React Application中启用CORS的最佳方法是什么? - What is the best way to enable CORS in React Application? 在 React 功能组件中声明 static 变量的最佳方法是什么? - What is the best way to declare static variables in React functional components? 在 React 应用程序中显示多个 static 图片的最佳方式是什么? - What's the best way to show multiple static images in a React app? 将验证码与 Spring Boot 和 React 应用程序一起使用的最佳方法是什么? - What can be the best way to use captcha with spring boot and react application? 将服务器数据提取到 React + Redux 应用程序的最佳方式 - The best way to fetch server data to your React + Redux application 将 React js 应用程序与节点 js 服务器连接的最佳方式是什么? - What is the best way to connect React js app with node js server? 使用服务器端渲染和react的最佳方法是什么? - what is the best way to use server side rendering with react? 在 React 中声明静态数据的最佳位置/方式 - Best place/way to declare static data in React 在 static 反应应用程序中传递图像的最佳方法 - Best way to pass images in static react app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM