Express.js unuse/remove vhost?

context

I have a server with a single IP and I have some virtual domains running on the same port using express.vhost.

app.listen (80);     
app.use (express.vhost ('www.example.com', 
                         require('./vhosts/example/app.js').app));    
app.use (express.vhost ('www.another.com', 
                         require('./vhosts/another/app.js').app));

app.js example

var express = require('express');
var app = express();
app.get('*', function(req, res){
  res.send('Hello Virtual Domain');
});
module.exports.app = app;

question

How I can release one of these virtual domains without restarting the application?

some kind of

app.use (var example = express.vhost ('www.example.com', 
                             require('./vhosts/example/app.js').app));
 // LATER
app.unuse(example);

I'm trying to outsource the administration of the vhosts and developing a tool for someone else can. This application monitors a json with a list of virtual domains file. I can easily add virtual domains but I need a way to remove them without restarting the service itself.

Express 4

You'll need the vhost module since Express 4 no longer depends on Connect.

With Express 4 you could create a router that handles all of your vhosts and isolate your vhosts from the rest of your routes.

var vhost = require('vhost');

var vhostRouter = express.Router();
vhostRouter.use(vhost('www.example.com', exampleApp));

You can then splice vhostRouter.stack to remove a vhost.

vhostRouter.stack

[ { handle: [Function: vhost],
    name: 'vhost',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: { /^\/?(?=\/|$)/i fast_slash: true },
    route: undefined } ]

If you are using the apps router you'll have to splice from app._router.stack


Express 3

req.use pushes middleware onto app.stack. Simply splice the vhost from app.stack


Note: This feature probably doesn't exist by design. I am not sure why. You should be aware though, as it is not supported and the behavior could change in a future version.