Node.js-在快递服务器上运行的BrowserSync [英] Nodejs - BrowserSync running on express server

查看:70
本文介绍了Node.js-在快递服务器上运行的BrowserSync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中提取URL并将其用作代理URL.但是,我使用的设置会使用递增的端口号为每个URL初始化一个新的BrowserSync服务器.

I want to pull a URL from the DB and use it as the proxied URL. However the setup I've come up with initializes a new BrowserSync server for each URL, using incrementing port numbers.

是否有一种无需每次都初始化新的BrowserSync服务器即可完成此操作的方法?

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

    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');
});

上面的方法很好,但是为每个URL(可能是数千个)初始化一个新服务器是一种好的做法吗?

The above is fine(ish) but is it good practice to be initializing a new server for every URL (potentially thousands)?

向系统的每个用户公开一个新的端口号是否安全?(我可以使用子域来屏蔽它吗?)

And is it safe to be exposing a new port number to every user of the system? (Can I mask this with a subdomain?)

我的最终目标是使用唯一的子域来引用每个代理URL.

My end goal is to use a unique subdomain to refer to each proxy url.

例如:

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

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

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

sub2.mysite.com proxies www.example2.com

推荐答案

浏览器同步无法正常工作,因为代理与服务器设置有关.

Browser-sync will not work as the proxy is tie to server setup.

我使用以下软件包:

  • 表达
  • express-http-proxy
  • 虚拟主机(表示虚拟主机)
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

这篇关于Node.js-在快递服务器上运行的BrowserSync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆