如何在加载时配置SPA? [英] How to configure a SPA on loading?

查看:173
本文介绍了如何在加载时配置SPA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Webpack,React,Node.JS,但我认为这个问题比特定技术更通用.在构建为开发模式或生产模式时(例如,使用DefinePlugin),我可以使用Webpack来配置SPA.

We are using Webpack, React, Node.JS but I think this question is more generic that the specific technologies. I can use Webpack to configure the SPA when building for development mode or production mode (e.g. using the DefinePlugin).

如何在生产模式(在构建时配置)下针对不同的部署环境(例如,登台与生产)配置SPA?例如,这些不同的部署将与不同的后端服务器API通信.

How can I configure a SPA in production mode (configured at build) for different deployment environments (e.g. staging vs production)? For example, these different deployments would talk to different backend server APIs.

当浏览器正在获取SPA时,SPA不得不以某种方式从服务器中获取一些本地上下文.还是我们必须在SPA可以安全地获取的每台服务器上都有一个自定义配置文件?

Somehow the SPA has to pickup some local context from the server as it is being GET'ed by the browser. Or perhaps we have to have a custom configuration file on each server that the SPA can securely GET?

我们在服务器上使用NodeJS,该SPA最终将作为同构应用程序运行,因此可能会有所帮助.我们正在Docker映像中部署这些应用程序,并且在部署时很容易配置它们的环境.

We are using NodeJS on the server and this SPA will eventually be running as an isomorphic app so that could help. We are deploying these applications in Docker images and it's easy to configure their environment on deployment.

谢谢您的建议.

推荐答案

我发现了一种完成要求的方法.服务SPA时,通过设置带有配置详细信息的cookie来实现.然后,SPA可以读取该cookie以获取配置(并删除该cookie,因为不再需要它).

I found one way of doing what is required. It is by setting a cookie with configuration details when serving the SPA. The SPA can then read that cookie to get the configuration (and delete the cookie because it is not needed any more).

有一个名为ClientConfig的NPM模块,它将帮助完成我所描述的事情.它的工作原理与名为GetConfig的NPM配套模块非常相似,后者有助于服务器端的配置. ClientConfig: https://github.com/henrikjoreteg/clientconfig

There is a NPM module called ClientConfig that will assist in doing what I have described. It works very similar to a companion NPM module called GetConfig that helps with configuration on the server side. ClientConfig: https://github.com/henrikjoreteg/clientconfig

此处介绍了如何分别(以及一起)使用ClientConfig和GetConfig: http://read.humanjavascript.com/ch12-settings-and-configs.html

How to use ClientConfig and GetConfig (separately and together) is described here: http://read.humanjavascript.com/ch12-settings-and-configs.html

对我来说,这似乎是一个解决方案,尽管我想知道是否存在任何潜在的安全性问题(这总是比第一次出现的情况更为复杂),以及是否没有更简单的方法.任何意见或进一步的解决方案将不胜感激.

This seems like a solution to me though I wonder about any potential security issues (that's alway more complex than first appears) and if there is not an easier approach. Any comments or further solution would be appreciated.

这篇关于如何在加载时配置SPA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆