Node.js 多个子域 [英] Node.js multiple subdomains

查看:55
本文介绍了Node.js 多个子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using node.js to build a multi-tenant application where different clients with their own subdomains will be accessing a single instance of my app. My questions is:

有没有办法让应用找出用户所在的子域?这样,我就可以将用户路由到正确的数据库架构 (postgresql).

Is there a way for the app to find out which subdomain a user is on? That way, I can route the user to the correct database schema (postgresql).

提前致谢!

附加信息:

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

myapp.client1domain.com

myapp.client1domain.com

myapp.client2domain.com

myapp.client2domain.com

myapp.client3domain.com

myapp.client3domain.com

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

Each of the above url's link to the same instance of the app. However, I need to know which subdomain a user is on so I can route them to the right database schema.

推荐答案

由于 HTTP/1.1 或更高版本中的host"在请求对象中反映为host"标头.你可以这样做:

Since "host" from HTTP/1.1 or greater is reflected in the request object as "host" header. You could do something like:

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

http.createServer((req, res) => {
    if (req.headers.host) {
        const parts = req.headers.host.split(":");
        setupDataBaseSchema(parts[0], parts[1]);
    }
});

请注意端口可能未定义;并进行额外检查,如果没有主机头或 HTTP 版本低于 1.1,则添加错误处理.当然,您可以像快速中间件一样做类似的事情,或者使用任何类似的框架,这只是裸 node.js http.

Please note that port might be undefined; and do additional checks, add error handling if there is no host header or HTTP version is below 1.1. Of course you could do similar as an express middleware, or with any similar framework, this is just bare node.js http.

更新:

在 express 我会做类似的事情:

In express I'd do something like:

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("host") 返回请求所指向的主机,例如myapp.client1domain.com 左右(将特定部分与正则表达式匹配)并基于此,您可以在 res.locals 上设置一个属性,您可以在后续路由中使用该属性,或者在未知域的情况下救助.

req.get("host") gives you back the host the request was directed to, e.g. myapp.client1domain.com or so (match specific part with regexp) and based on that you could set a property on res.locals which you could use on subsequent routes, or bail out in case of an unknown domain.

如果您向 http://localhost:<port>/db 发出请求,上面的代码将记录与主机的连接:localhost".

The above snipped would log "a connection to scheme by host: localhost", if you do a request to http://localhost:<port>/db.

这篇关于Node.js 多个子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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