如何在Express.js或Connect.js中配置多个子域 [英] How can I configure multiple sub domains in Express.js or Connect.js

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

问题描述

我习惯于使用httpd(Apache),它提供了一种配置映射到目录的子域的方法。
如何在Connect.js / Express.js中做同样的事情?我看到我唯一的是我不知道如何配置子域的路由。我有子域名,如m.mysite.com,sync.mysite.com

I am used to working on httpd ( Apache ) which provides a way to configure subdomains which is mapped to a directory. How can I do the same thing in Connect.js/Express.js ? I see that the only thing that I have is routes which I am not sure how I can use to configure sub domains. I have subdomains like m.mysite.com, sync.mysite.com

有人可以帮助吗?

推荐答案

或者您可以使用 vhost

然后,创建几个网站他们自己的目录和出口的快递应用程序,例如。 /path/to/m/index.js

Then, create several sites in their own directory and export the express app, eg. /path/to/m/index.js:

var app = express()
/* whatever configuration code */
exports.app = app
// There is no need for .listen()

然后使用以下应用程序处理所有请求:

And then handle all requests with the following app:

var vhost = require('vhost');

express()
.use(vhost('m.mysite.com', require('/path/to/m').app))
.use(vhost('sync.mysite.com', require('/path/to/sync').app))
.listen(80)

请注意, / path / to / m / path / to / sync 可以是绝对路径(如上所述)或相对路径。

Note that /path/to/m and /path/to/sync can be absolute paths (as written above) or relative paths.

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

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