node.js 如何在 Express.js 或 Connect.js 中配置多个子域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5791260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I configure multiple sub domains in Express.js or Connect.js
提问by Raks
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
我习惯于使用 httpd ( Apache ),它提供了一种配置映射到目录的子域的方法。我如何在 Connect.js/Express.js 中做同样的事情?我看到我唯一拥有的是路由,我不确定如何使用它来配置子域。我有像 m.mysite.com、sync.mysite.com 这样的子域
Can someone help ?
有人可以帮忙吗?
回答by Adrien
Or alternatively you could use vhost.
或者,您可以使用vhost.
Then, create several sites in their own directory and export the express app, eg. /path/to/m/index.js:
然后,在自己的目录中创建多个站点并导出 express 应用程序,例如。/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)
Note that /path/to/mand /path/to/synccan be absolute paths (as written above) or relative paths.
请注意,/path/to/mand/path/to/sync可以是绝对路径(如上所述)或相对路径。
回答by neebz
You could append a subdomain to a request and then check for it in subsequent next()calls.
您可以将子域附加到请求,然后在后续next()调用中检查它。
I got the following code from > http://groups.google.com/group/express-js/browse_thread/thread/b04bbaea7f0e8eed(so full credit to the original author)
我从 > http://groups.google.com/group/express-js/browse_thread/thread/b04bbaea7f0e8eed得到以下代码(非常感谢原作者)
app.get('*', function(req, res, next){
if(req.headers.host == 'some.sub.domain.com') //if it's a sub-domain
req.url = '/mysubdomain' + req.url; //append some text yourself
next();
});
// This will mean that all get requests that come from the subdomain will get
// /subdomain appended to them, so then you can have routes like this
app.get('/blogposts', function(){
// for non-subdomain
});
app.get('/mysubdomain/blogposts', function(){
// for subdomain
});
回答by bmullan91
I have recently came across this problem, and wrote a module to help with it using express 4. https://www.npmjs.org/package/express-subdomain.
我最近遇到了这个问题,并使用 express 4. https://www.npmjs.org/package/express-subdomain编写了一个模块来帮助解决它。
Example - api subdomain.
示例 - api 子域。
var express = require('express');
var app = express();
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/users', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
app.use(subdomain('api', router));
app.listen(3000);
Check out the module on npm to see more examples.
查看 npm 上的模块以查看更多示例。
回答by wilsonpage
I created a module to help with subdomains in Express: https://github.com/WilsonPage/express-subdomain-handler
我创建了一个模块来帮助 Express 中的子域:https: //github.com/WilsonPage/express-subdomain-handler

