使用代理中间件与gulp connect

时间:2014-07-03 06:30:09

标签: javascript gulp

我正在尝试使用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变量。

4 个答案:

答案 0 :(得分:24)

chimurai Github给出的解决方案是使用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']);

答案 1 :(得分:7)

即使使用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]'
        ])
      ];
    }
  });
});

答案 2 :(得分:3)

答案 3 :(得分:3)

这是我使用livereload启动快速服务器(使用nodemon)并代理服务器的所有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');
        });
});