Orane Orane - 2 months ago 27
Node.js Question

Nodejs - Dynamic express subdomains with vhost

Id like to dynamically create subdomains for different proxies, like so:

var app = require('express')();
var proxy = require('express-http-proxy');
var vhost = require('vhost');

app.get('/make', function (req, res) {
app.use(vhost('sub1.mysite.com', proxy("www.example.com")));
});
app.listen(8080);


What's the best way to accomplish this?
The problem seems to be calling app.use() after the server is created.

Answer

app.get should send back something to the client else the client will retry and then timeout.

var app = require('express')();
var proxy = require('express-http-proxy');
var vhost = require('vhost');

app.get('/make', function (req, res) {
    app.use(vhost('sub1.mysite.com', proxy("www.example.com")));

    res.send('Created'); // <----- Send reply to client
});
app.listen(8080);