Node.js多个子域

Node.js多个子域,node.js,subdomain,multi-tenant,Node.js,Subdomain,Multi Tenant,我正在使用node.js构建一个多租户应用程序,其中具有自己子域的不同客户端将访问我的应用程序的单个实例。我的问题是: 应用程序是否有办法找出用户所在的子域?这样,我就可以将用户路由到正确的数据库模式(postgresql) 提前谢谢 其他信息: 我正在使用Express框架 我不使用多个实例,因为我希望有数千个用户,我不想管理数千个实例 关于子域,我的意思是: myapp.client1domain.com myapp.client2domain.com myapp.client3doma

我正在使用node.js构建一个多租户应用程序,其中具有自己子域的不同客户端将访问我的应用程序的单个实例。我的问题是:

应用程序是否有办法找出用户所在的子域?这样,我就可以将用户路由到正确的数据库模式(postgresql)

提前谢谢

其他信息:

  • 我正在使用Express框架
  • 我不使用多个实例,因为我希望有数千个用户,我不想管理数千个实例
  • 关于子域,我的意思是:
myapp.client1domain.com

myapp.client2domain.com

myapp.client3domain.com

上面每个url的链接都指向同一个应用程序实例。但是,我需要知道用户在哪个子域上,以便将它们路由到正确的数据库架构。

因为HTTP/1.1或更高版本中的“主机”作为“主机”头反映在请求对象中。你可以这样做:

const setupDatabaseScheme = (host, port) => {
  // ...
};

http.createServer((req, res) => {
    if (req.headers.host) {
        const parts = req.headers.host.split(":");
        setupDataBaseSchema(parts[0], parts[1]);
    }
});
const getConnecitonForSchemeByHost = (host) => {
    // ... get specific connection for given scheme from pool or similar
    return "a connection to scheme by host: " + host;
};

router
    .all("*", function (req, res, next) {
        const domain = req.get("host").split(":")[0];
        const conn = res.locals.dbConnection = getConnecitonForSchemeByHost(domain);
        if (conn) {
            next();
        } else {
            next(new Error("no connection for domain: " + domain))
        }
    })
    .get("/", function (req, res) { // use connection from res.locals at further routes
        console.log(res.locals.dbConnection);
        res.send("ok");
    });

app.use("/db", router);
请注意,端口可能未定义;如果没有主机头或HTTP版本低于1.1,则执行其他检查,添加错误处理。当然,您可以像express中间件一样,或者使用任何类似的框架,这只是bare node.js http

更新:

在express中,我会这样做:

const setupDatabaseScheme = (host, port) => {
  // ...
};

http.createServer((req, res) => {
    if (req.headers.host) {
        const parts = req.headers.host.split(":");
        setupDataBaseSchema(parts[0], parts[1]);
    }
});
const getConnecitonForSchemeByHost = (host) => {
    // ... get specific connection for given scheme from pool or similar
    return "a connection to scheme by host: " + host;
};

router
    .all("*", function (req, res, next) {
        const domain = req.get("host").split(":")[0];
        const conn = res.locals.dbConnection = getConnecitonForSchemeByHost(domain);
        if (conn) {
            next();
        } else {
            next(new Error("no connection for domain: " + domain))
        }
    })
    .get("/", function (req, res) { // use connection from res.locals at further routes
        console.log(res.locals.dbConnection);
        res.send("ok");
    });

app.use("/db", router);
req.get(“主机”)
返回请求指向的主机,例如
myapp.client1domain.com
左右(将特定部分与regexp匹配),基于此,您可以在
res.locals
上设置属性,以便在后续路由中使用,或者在未知域的情况下退出


如果您请求
http://localhost:/db

请精确说明如何处理连接。如果你要求“子域”,这意味着你有一个Web服务器?如果是,您使用的是witch web框架吗<代码>Sails.js<代码>快车
total.js
?不使用多个实例有什么特殊原因吗?@Ifnot我更新了我的问题以解决您的问题comment@Yerken我更新了我的问题以回应您的评论。根据您添加的详细信息更新了我的答案,希望可以