我可以在单独的模块中路由快递控制器和基本Nodejs服务器

我有一个要求。 取决于使用不同模块的HOST头,就像使用expressjs的www.myhost.com和使用基本nodejs https.createServer()的* .h.myhost.com。 他们在同一个港口工作。

https.createServer(options,function(req, res){ if(req.host === "www.myhost.com"){ express.handle(req,res) //what I hope return } //handle by normal way }) 

这个怎么做?

你可以用nodejitsu来使用node-http-proxy 。 我使用它来部署和configuration在不同子域下运行的多个应用程序。

例:

 var express = require('express'), https = require('https'), proxy = require('http-proxy'); // define proxy routes var options = { router: { 'www.myhost.com': '127.0.0.1:8001', '*.h.myhost.com': '127.0.0.1:8002' } }; // express server for www.myhost.com var express = express.createServer(); // register routes, configure instance here // express.get('/', function(res, req) { }); // start express server express.listen(8001); // vanilla node server for *.h.myhost.com var vanilla = https.createServer(options,function(req, res){ // handle your *.h.myhost.com requests }).listen(8002); // start proxy var proxyServer = httpProxy.createServer(options); proxyServer.listen(80); 

我不确定在http代理路由表(* .h.myhost.com)中使用通配符,但是由于这些值被转换为node-http-proxy中的正则expression式,我假定它们工作。