简体   繁体   English

Node.js-在Express服务器上运行的BrowserSync

[英]Nodejs - BrowserSync running on express server

I want to pull a URL from the DB and use it as the proxied URL. 我想从数据库中提取一个URL并将其用作代理URL。 However the setup I've come up with initializes a new BrowserSync server for each URL, using incrementing port numbers. 但是,我提出的设置使用递增的端口号为每个URL初始化了一个新的BrowserSync服务器。

Is there a way to accomplish this without initializing a new BrowserSync server every time? 有没有一种方法可以在不每次都初始化新的BrowserSync服务器的情况下完成此任务?

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

    var proxyUrl = getUrl() //get url from db (www.example.com)

    bs.create("bs1").init({
        notify: false,
        open: false,
        ui: false,
        port: 10000,
        proxy: proxyUrl
    });
        res.send();
});

app.use(router);

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

The above is fine(ish) but is it good practice to be initializing a new server for every URL (potentially thousands)? 上面的方法很不错,但是为每个URL初始化一个新服务器(可能是数千个)是否是一个好习惯?

And is it safe to be exposing a new port number to every user of the system? 向系统的每个用户公开一个新的端口号是否安全? (Can I mask this with a subdomain?) (我可以使用子域来屏蔽它吗?)

Update 更新资料

My end goal is to use a unique subdomain to refer to each proxy url. 我的最终目标是使用唯一的子域来引用每个代理URL。

For example: 例如:

sub1.mysite.com proxies www.example.com, sub1.mysite.com代理www.example.com,

sub2.mysite.com proxies www.example2.com sub2.mysite.com代理www.example2.com

Browser-sync will not work as the proxy is tie to server setup. 浏览器同步将不起作用,因为代理与服务器设置有关。

I use following packages: 我使用以下软件包:

  • express 表达
  • express-http-proxy 快递-http-代理
  • vhost (express vhost) 虚拟主机(表达虚拟主机)
const port = 8080;

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

app.listen(port);

/* Assuming getUrl() will return an array of sites */
// var sites = getUrl();

// DO NOT put '/' at the end of site
var sites = [
    'http://www.bing.com',
    'http://samanthagooden.com',
    'http://www.courtleigh.com'
];

var i = 0;
sites.forEach(site => {
    i++;
    var subDomain = 'sub' + i + '.mysite.com';
    app.use(vhost(subDomain, proxy(site, {
        forwardPath: (req, res) => url.parse(req.url).path,
        intercept: (rsp, data, req, res, callback) => {
            if (res._headers['content-type']) {
                var contentType = res._headers['content-type'];
                if (
                    contentType.indexOf('text') !== -1 ||
                    contentType.indexOf('javascript') !== -1
                ) {
                    // Replace link if content-type = text or javascript
                    var reg = new RegExp(site, 'g');
                    res.send(data.toString().replace(reg, ''));
                } else {
                    res.send(data);
                }
            } else {
                res.send(data);
            }
        }
    })));
    console.log(subDomain + ':' + port + ' proxy: ' + site);
});

The above example will create following proxies: 上面的示例将创建以下代理:

sub1.mysite.com:8080 proxy: www.bing.com
sub2.mysite.com:8080 proxy: www.example.com

Maybe I'm misunderstanding what you are trying to do, but Browsersync and express seems a bit overkill in this case, why not just use node-http-proxy with the native http module? 也许我误会了您要做什么,但是在这种情况下,Browsersync和express似乎有点矫kill过正,为什么不只将node-http-proxy与本机http模块一起使用?

var http = require('http')
var httpProxy = require('http-proxy')

var options = ...
var proxy = httpProxy.createProxyServer(options)

var server = http.createServer(function (req, res) {
  var proxyUrl = getUrl()
  proxy.web(req, res, { target: proxyUrl })
})

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

As per me If you want SAAS service using proxy is not the good idea to go is what am thinking.. if you are going with proxy for each client will create process with new port... My Solution is to create node server with listen localhost and map *.domain.com to the server.. 按照我的说法,如果要使用代理的SAAS服务不是一个好主意,那是在想什么..如果您要为每个客户端使用代理,则将使用新端口创建进程...我的解决方案是使用监听创建节点服务器本地主机并将* .domain.com映射到服务器。

If you are using individual database for each client :- in node logic get cname from request host and use that reference to connect database. 如果对每个客户端使用单独的数据库,则:-在节点逻辑中,从请求主机获取cname并使用该引用连接数据库。

Final Controller code would be.. var express = require('express'); 最终的控制器代码将是.. var express = require('express'); var router = express.Router(); var router = express.Router(); var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb')。MongoClient;

/* GET home page. */
router.get('/', function(req, res, next) {
        var client = req.subdomains[0];
        console.log(client);
        MongoClient.connect('mongodb://localhost:27017/'+client, function(err, db) {
  if (err) {
    throw err;
  }
  db.collection('app1').find().toArray(function(err, result) {
    if (err) {
      throw err;
    }
        console.log('data');
    console.log(result);
  });
});

  res.render('index', { title: 'Express' });
});

module.exports = router;
~                                                                                                                                              
~                                  

In future if you get more clients you can implement node cluster or standard Ubuntu cluster using webservice 将来如果您有更多的客户端,则可以使用Web服务实现节点群集或标准Ubuntu群集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM