开发者

Can I route express controllers and basic nodejs server in seperate modules

开发者 https://www.devze.com 2023-02-15 10:05 出处:网络
I have a requirement. denpends on the HOST header using different module,just likewww.m开发者_开发问答yhost.com using expressjs, and *.h.myhost.com using basic nodejs https.createServer(). And they ar

I have a requirement. denpends on the HOST header using different module, just like www.m开发者_开发问答yhost.com using expressjs, and *.h.myhost.com using basic nodejs https.createServer(). And they are works in same port.

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

How to do this?


You could use node-http-proxy by nodejitsu. I use it to deploy and configure multiple applications running under different subdomains.

Example:

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);

I'm not sure about using wildcards in the http-proxy routing table (*.h.myhost.com), but since these values are converted to regular expressions in node-http-proxy, i assume they work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号