简体   繁体   English

使用代理中间件与gulp connect

[英]Use proxy middleware with gulp connect

I am trying to use gulp-connect to forward all requests to api/ to localhost:3000 . 我正在尝试使用gulp-connect将所有请求转发到api/ localhost:3000 I found an example at https://github.com/AveVlad/gulp-connect/issues/27 我在https://github.com/AveVlad/gulp-connect/issues/27上找到了一个例子

and setup my connect task like this: 并设置我的连接任务,如下所示:

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);
      })()]
    }
    });
});

Running this task warns that connect deprecated connect(middleware): use app.use(middleware) instead node_modules/gulp-connect/index.js:39:19 and this task does not forward requests as expected. 运行此任务警告connect deprecated connect(middleware): use app.use(middleware) instead node_modules/gulp-connect/index.js:39:19并且此任务不按预期转发请求。

I looked at the connect source to see if I could work around the depreciation, but it is beyond my level in js: 我查看了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);
    }
  };

I cannot figure out how to change my gulp-file to use app.use(middleware) , the app variable is not exported by the connect module. 我无法弄清楚如何更改我的app.use(middleware)文件以使用app.use(middleware) ,连接模块不会导出app变量。

A solution given by chimurai on Github is to use the http-proxy-middleware package to do that. chimuraiGithub上给出的解决方案是使用http-proxy-middleware包来做到这一点。

For example : 例如 :

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

I couldn't get middleware to work correctly even when using the github source. 即使使用github源代码,我也无法使中间件正常工作。 I did get the same result with modrewrite 我确实用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]'
        ])
      ];
    }
  });
});

This is my config to start an express server with livereload (using nodemon) and proxy all API requests to the server. 这是我的配置,用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');
        });
});

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

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