简体   繁体   English

如何为连接到它的每个前端应用程序提供相同的后端但使用不同的数据库

[英]How to serve the same backend but with different database for every frontend app that is connecting to it

I'm looking to deploy my backend on a server and to have multiple frontend apps connected to it, but for every one to have different database, is there any way to handle this?我希望在服务器上部署我的后端并连接多个前端应用程序,但是每个人都有不同的数据库,有没有办法处理这个问题? I am using Node, express, mongoose, mongodb.我正在使用节点,快递,mongoose,mongodb。 The logic of the backend should be the same, but I need different db for every frontend app connected to backend.后端的逻辑应该是相同的,但我需要为每个连接到后端的前端应用程序使用不同的数据库。

Thanks and I hope this is appropriate to ask in this forum, otherwise please do let me know where I can find more information about this.谢谢,我希望在这个论坛上提问是合适的,否则请让我知道在哪里可以找到有关此的更多信息。

You can find the domain that the client tried to access in the request's Host header.您可以在请求的Host header 中找到客户端尝试访问的域。 If every app connects to a different domain, you can use that to differentiate them.如果每个应用程序都连接到不同的域,您可以使用它来区分它们。 Otherwise, you could just send a query parameter containing the app name or identifier, and differentiate them through that.否则,您可以只发送一个包含应用名称或标识符的查询参数,并以此区分它们。

Then, when you start the application, you could have something like an object with the application name or identifier as the key and a database connection as the value, and choose the database connection to interact with based on the application that made the request.然后,当您启动应用程序时,您可能会拥有类似 object 的内容,其中应用程序名称或标识符作为键,数据库连接作为值,并根据发出请求的应用程序选择要与之交互的数据库连接。

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

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