简体   繁体   English

在 NodeJS Server 中为多个客户提供多个生产环境

[英]Have multiple production environment for multiple customers in NodeJS Server

After researching a lot I can't find anything similar to help me solve this problem.经过大量研究后,我找不到任何类似的东西来帮助我解决这个问题。

I have a Node server, with several environments (dev, test, demo, prod).我有一个节点服务器,有几个环境(开发、测试、演示、产品)。 This server is deployed in production on a Linux server, via a service.该服务器通过服务部署在 Linux 服务器上的生产环境中。

I need to be able to have several production environments for several different customers.我需要能够为几个不同的客户提供多个生产环境。

Example: I have 2 urls: https://customer1.com and https://customer2.com .示例:我有 2 个网址: https://customer1.comhttps://customer2.Z20C50Z6D1950DA4 The code of these two clients are identical, only the url changes.这两个客户端的代码是相同的,只是 url 改变了。

For the server, it must be able to recognize which client is sending it a request, because the data to be sent back to the client is not the same.对于服务器来说,它必须能够识别哪个客户端正在向它发送请求,因为要发送回客户端的数据是不一样的。 The customer1 will have its database on a different url than that of customer2. customer1 的数据库与 customer2 的数据库不同。 The server will therefore have to make the distinction in order to return only the data concerning the client making the request.因此,服务器必须进行区分,以便仅返回与发出请求的客户端有关的数据。

My question: how to achieve this?我的问题:如何实现这一目标? I would like to avoid deploying 1 server per client, which I know would be simpler, but less maintainable.我想避免为每个客户端部署 1 个服务器,我知道这会更简单,但可维护性较差。

Currently, I am using Express-Session to define environments.目前,我正在使用 Express-Session 来定义环境。 In fact, I have a middleware which will look in mysql for the environment variables of each client:事实上,我有一个中间件,它将在 mysql 中查找每个客户端的环境变量:

 con.connect(function(err) {
      if (err) throw err;
      con.query(`SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}'`, function(err, result) {
        if (err) throw err;
        delete result[0].ID;
        for (var prop in result[0]) {
          req.session[prop] = result[0][prop];
        }
        next();
      });
      con.end();
    });

It seems to work but it doesn't seem very stable or very reliable to me, am I wrong?它似乎有效,但对我来说似乎不是很稳定或很可靠,我错了吗?

What better can I use to separate the data so that there is no customer1 that can receive the data from customer2?我可以使用什么更好的方法来分离数据,以便没有 customer1 可以接收来自 customer2 的数据?

Thank you for your help!谢谢您的帮助!

Following all comments under your original post, you need to do something like this:在原始帖子下的所有评论之后,您需要执行以下操作:

SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}' AND CUSTOMER_NAME 
LIKE yourUserCustomerFromSession

Before, any user could query data for any customer as long as they use the URL for that customer, now this is no longer possible.以前,任何用户只要使用 URL 就可以查询任何客户的数据,现在已经不可能了。

Even better way of doing it, if you don't want to hold the Client name in the session, you can do 2 queries - the first one to get the Client name for the logged in User and the second one similar to the code above:更好的方法是,如果您不想在 session 中保留客户端名称,您可以执行 2 个查询 - 第一个查询获取登录用户的客户端名称,第二个查询类似于上面的代码:

SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}' AND 
CUSTOMER_NAME LIKE theClientNameYouJustGotForTheLoggedInUser

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

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