Orane Orane - 2 months ago 12
Node.js Question

Nodejs - Disable or destroy vhost subdomains/proxies

I have the follow code which dynamically proxies a website to a subdomain.

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("Proxy Created");
});

app.get('/destroy', function (req, res) {

// Disable or Destroy the subdomain/proxy created above
// ???

});
app.listen(8080);


How would I disable or destroy this newly created proxy/subdomain from the
/destroy
route?

Answer

app.js

var Ex = require('express');
var app = Ex();
app.listen(8080);

var DomainProxy = require('./DomainProxy').DomainProxy;
// Pass express instance 'app' into dp,
// dp will create a vhost router internally
var dp = new DomainProxy(app);

app.get('/make', function (req, res) {
    var subDomain = 'sub1.mysite.com';
    var site = 'http://www.example.com'; // site need to contain 'http://', else dp link rewrite will not work correctly

    dp.create(subDomain,site);

    res.send("Proxy Created");
});

app.get('/destroy', function (req, res) {

    var subDomain = 'sub1.mysite.com';

    dp.destroy(subDomain);

    res.send("Proxy Destroyed");

});

DomainProxy.js

I also incorporated my answer from Nodejs - BrowserSync running on express server

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

var DomainProxy = (function () {
    function DomainProxy(app) { // DomainProxy
        this.list = []; // vhost list array
        this.router = Ex.Router();
        app.use(this.router);
    }

    DomainProxy.prototype.create = function (domain, site) {

        this.list.push(domain); // Add router, domain pair into list

        this.router.use(vhost(domain, proxy(site, {
            forwarDomainProxyath: (req, res) => url.parse(req.url).path,
            intercept: (rsp, data, req, res, callback) => {
                if (res._headers['content-type']) {
                    var contentType = res._headers['content-type'];
                    // console.log(contentType);
                    if (
                        contentType.indexOf('text') !== -1 ||
                        contentType.indexOf('javascript') !== -1
                    ) {
                        // Rewrite links
                        var reg = new RegExp(site, 'g');
                        res.send(data.toString().replace(reg, ''));
                    } else {
                        res.send(data);
                    }
                } else {
                    res.send(data);
                }
            }
        })));
    };

    DomainProxy.prototype.destroy = function (domain) {
        var i = this.list.indexOf(domain);
        this.list.splice(i, 1);
        this.router.stack.splice(i, 1);
    };
    return DomainProxy;
} ());
exports.DomainProxy = DomainProxy;
Comments