Orane Orane - 2 months ago 24
Node.js Question

Express with Browser-Sync

I want to integrate BrowserSync with express.

I want to pull a URL from the DB and use it as the proxy. However the setup I've come up with initializes a new BrowserSync server for each request.

Is there a way to accomplish this without initializing a new BrowserSync server every time?

Or should I be using another approach?

var bs = require("browser-sync");
var express = require("express");

var router = express.Router();
var app = express();

router.get("/", function(req, res){
bs.create("bs1").init({
notify: false,
open: false,
ui: false,
port: 10000,
proxy: 'http://example.com'
});
res.send();
});

app.use(router);

app.listen(8080, function(){
console.log('listening on *:8080');
});

Answer

So it turns out this cant actually be done with any ease.

I ended up using express-http-proxy instead and express-vhost for subdomains.

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

app.use(vhost('sub1.mysite.com', proxy('http://www.example.com')));

app.listen(8080);

Hopefully this helps someone one day.

Comments