Javascript 使用Grunt Connect和Livereload配置多台服务器

Javascript 使用Grunt Connect和Livereload配置多台服务器,javascript,gruntjs,Javascript,Gruntjs,我正在尝试使用2台服务器执行grunt连接任务。虽然它在文档中有解释,但我正在使用livereload启动它,我无法使它工作 connect : { proxies : [ { context : '/rest', host : 'localhost', port : 8080, https : false, changeOrigin : false,

我正在尝试使用2台服务器执行grunt连接任务。虽然它在文档中有解释,但我正在使用livereload启动它,我无法使它工作

    connect : {
        proxies : [ {
            context : '/rest',
            host : 'localhost',
            port : 8080,
            https : false,
            changeOrigin : false,
            rewrite : {
                'rest' : 'paf/rest'
            }
        }, {
            context : '/logout',
            host : 'localhost',
            port : 8080,
            https : false,
            changeOrigin : false,
            rewrite : {
                'logout' : 'paf/logout'
            }
        } ],
        options : {
            base : 'build',
            port : 9000,
            hostname : '0.0.0.0'
        },
        livereload : {
            options : {
                middleware : function(connect) {
                    return [ proxySnippet, lrSnippet, mountFolder(connect, '../target/build') ];
                }
            }
        }
    }
要启动我的服务器,我使用:

grunt.registerTask('server', [ 'configureProxies', 'connect:livereload', 'watch' ]);
我的另一台服务器几乎使用相同的配置,只需将“build”路径替换为“bin”。 我试图按照文档中的2个服务器声明进行操作,但随后无法正确启动它

    connect : {
        dev: {
            proxies : [ { ...}],
            options : { ... },
            livereload : {}
        },
        prod: {
            proxies : [ { ...}],
            options : { ... },
            livereload : {}
        }
    }


grunt.registerTask('serverDev', [ 'configureProxies', 'connect:dev:livereload', 'watch' ]);
grunt.registerTask('serverProd', [ 'configureProxies', 'connect:prod:livereload', 'watch' ]);
但是,它只调用connect:dev,而不是livereload。 我一直在考虑connect的多个任务,但设置起来似乎很复杂。

主要问题是您必须确保connect/livereload端口不会在每个设置之间发生冲突。 下面是我做的最简单的工作示例:

'use strict';

module.exports = function (grunt) {

    require('time-grunt')(grunt);
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        connect: {
            options: {
                open: true,
                hostname: 'localhost'
            },
            first: {
                options: {
                    port: 8000,
                    livereload: 3500,
                    base: './'
                }
            },
            second: {
                options: {
                    open: {
                        target: 'http://<%= connect.options.hostname %>:<%= connect.second.options.port %>/two.html'
                    },
                    port: 8001,
                    livereload: 3501,
                    base: './',
                    index: 'two.html'
                }
            }
        },
        watch: {
            first: {
                options: { livereload: 3500 },
                files: ['style.css', 'index.html']
            },
            second: {
                options: { livereload: 3501 },
                files: ['style.css', 'two.html']
            },
        }
    });

    grunt.registerTask('default', [
        'connect:first',
        'watch:first'
    ]);

    grunt.registerTask('second', [
        'connect:second',
        'watch:second'
    ]);
};
“严格使用”;
module.exports=函数(grunt){
要求(“时间咕噜”)(咕噜);
要求('load-grunt-tasks')(grunt);
grunt.initConfig({
连接:{
选项:{
开放:是的,
主机名:“localhost”
},
第一:{
选项:{
港口:8000,
利弗雷罗德:3500,
基数:'./'
}
},
第二:{
选项:{
开放式:{
目标:“http://:/two.html”
},
港口:8001,
利弗雷罗德:3501,
基数:“./”,
索引:“two.html”
}
}
},
观察:{
第一:{
选项:{livereload:3500},
文件:['style.css','index.html']
},
第二:{
选项:{livereload:3501},
文件:['style.css','two.html']
},
}
});
grunt.registerTask('default'[
'连接:第一',
“注意:第一”
]);
grunt.registerTask('second'[
'连接:第二',
“手表:第二”
]);
};
然而,我在使用更复杂的连接设置(中间件等)时遇到了问题。我遇到的最大障碍是注入正确的livereload端口?我还不能确定

更新 中间件+自定义livereload端口阻塞可能是一个已知问题

发件人:

是的,这种行为是有原因的。connect livereload中间件重写res.write、res.writeHead和res.end并存储原始版本