【问题标题】:Use proxy middleware with gulp connect通过 gulp connect 使用代理中间件
【发布时间】:2014-07-03 06:30:08
【问题描述】:

我正在尝试使用gulp-connect 将所有请求转发到api/localhost:3000。我在https://github.com/AveVlad/gulp-connect/issues/27找到了一个例子

并像这样设置我的连接任务:

gulp.task('connect', function(){
    connect.server({
        root: './app', 
    middleware: function(connect, o) {
      return [ (function() {
        var url = require('url');
        var proxy = require('proxy-middleware');
        var options = url.parse('http://localhost:3000/api');
        options.route = 'api';
        return proxy(options);
      })()]
    }
    });
});

运行此任务会警告connect deprecated connect(middleware): use app.use(middleware) instead node_modules/gulp-connect/index.js:39:19,并且此任务不会按预期转发请求。

我查看了connect 来源,看看是否可以解决折旧问题,但这超出了我在 js 中的水平:

 ConnectApp.prototype.server = function() {
    var app, middleware;
    middleware = this.middleware();
    app = connect.apply(null, middleware);
    server = http.createServer(app);
    app.use(connect.directory(typeof opt.root === "object" ? opt.root[0] : opt.root));
    server.listen(opt.port);
    this.log("Server started http://" + opt.host + ":" + opt.port);
    if (opt.livereload) {
      tiny_lr.Server.prototype.error = function() {};
      lr = tiny_lr();
      lr.listen(opt.livereload.port);
      return this.log("LiveReload started on port " + opt.livereload.port);
    }
  };

我不知道如何更改我的 gulp 文件以使用 app.use(middleware)app 变量未由连接模块导出。

【问题讨论】:

    标签: javascript gulp


    【解决方案1】:

    chimuraiGithub 给出的解决方案是使用http-proxy-middleware 包来做到这一点。

    例如:

    var gulp = require('gulp');
    var connect = require('gulp-connect');
    var proxy = require('http-proxy-middleware');
    
    gulp.task('connect', function() {
        connect.server({
            root: ['./app'],
            middleware: function(connect, opt) {
                return [
                    proxy('/api', {
                        target: 'http://localhost:3000',
                        changeOrigin:true
                    })
                ]
            }
    
        });
    });
    
    gulp.task('default', ['connect']);
    

    【讨论】:

      【解决方案2】:

      即使使用 github 源,我也无法让中间件正常工作。我确实得到了与modrewrite 相同的结果

      var modRewrite = require('connect-modrewrite');
      
      gulp.task('connect', function() {
        connect.server({
          root: './app',
          port: 8000,
          middleware: function() {
            return [
              modRewrite([
                '^/api/(.*)$ http://localhost:3000/api/v1/$1 [P]'
              ])
            ];
          }
        });
      });
      

      【讨论】:

        【解决方案3】:

        这是一个报告的问题,应该在 NPM 的下一个 gulp-connect 版本中修复:

        https://github.com/AveVlad/gulp-connect/commit/9bd7da765d6763bbee566cc5fc03b873ccf93e37 https://github.com/AveVlad/gulp-connect/issues/67

        【讨论】:

        • 是否可以同时使用来自 github 的构建,还是不推荐?
        • 在你的 package.json 依赖映射中,你可以直接指向一个 github repo。
        【解决方案4】:

        这是我使用 livereload(使用 nodemon)启动 express 服务器并将所有 API 请求代理到服务器的配置。

        gulp.task('connect', function () {
            var connect = require('connect');
        
            // Start the Node server to provide the API
            var nodemon = require('gulp-nodemon');
            nodemon({ cwd: '../server', script: 'app.js', ignore: ['node_modules/*'], ext: 'js' })
                .on('restart', function () {
                    console.log('Node server restarted!')
                });
        
        
            var app = connect()
                // proxy API requests to the node server
                .use(require('connect-modrewrite')(['^/api/(.*)$ http://localhost:3000/api/$1 [P]']))
                .use(require('connect-livereload')({ port: 35729 }))
                .use(connect.static('../client'))
                .use(connect.static('../client/.tmp'))
                .use(connect.directory('../client'));
        
            require('http').createServer(app)
                .listen(9000)
                .on('listening', function () {
                    console.log('Started connect web server on http://localhost:9000');
                });
        });
        

        【讨论】:

          最近更新 更多