11

如何使用 webpack-dev-server实现“代理”(类似于grunt-connect-proxy )选项?

我正在将webpackwebpack-dev-server与 Grunt 一起使用。Gruntfile.js(下面的代码)中的任务能够在端口 8080 上启动服务器。我想为所有后端数据请求(上下文 URL /ajax/*)添加代理设置。

       "webpack-dev-server": {
        options: {
            webpack: webpackConfig,
            publicPath: "/src/assets"
        },
        start: {
            keepAlive: true,
            watch: true              
        }
    } 
4

4 回答 4

22

在 webpack 配置中,您可以像这样使用 devServer.proxy:

proxy: {
    '/ajax/*': 'http://your.backend/'
}
于 2015-06-12T13:06:40.030 回答
1

我最终将 'grunt-contrib-connect' 和 'grunt-connect-proxy' 与 'webpack-dev-middleware' 一起使用。因此,我可以使用代理中间件来处理我的所有数据请求,并使用 webpack 中间件来处理静态捆绑文件请求。

    var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
    var mountFolder = function (connect, dir) {
       return connect.static(require('path').resolve(dir));
    };

    var prepareDevWebpackMiddleware = function() {

        webpackConfig.devtool = "eval-source-map";
        var compiler = webpack(require("./webpack.config.js"));

        return webpackDevMiddleware(compiler, {
            publicPath : "/assets"           
       });
    };

---- 咕噜任务 ----

        connect: {
            options: {
                port: 8080,
                hostname: 'localhost',
                livereload : true
            },
            proxies: [{
                context: '/api',
                host: 'localhost',
                port: 8000
            }],
            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            prepareDevWebpackMiddleware(),
                            proxySnippet,
                            mountFolder(connect, 'src')
                        ];
                    }
                }
            }
      }
于 2014-12-16T19:22:51.160 回答
1

Webpack 开发服务器代理 api 自 v1.15 起已更改

https://github.com/webpack/webpack-dev-server/issues/562

glob * 应该是 ** 来代理所有请求

  devServer: {
    proxy: {
      '**': 'http://local.ui.steelhouse.com/'
    },
  }
于 2016-09-01T19:49:06.147 回答
0

webpack-dev-server 不知道如何处理您的内容,因此它有一个配置可以将您的所有请求代理到特定的服务器处理内容。

例如:

你应该运行“grunt content”来启动你的内容服务器,然后运行“grunt serve”来开始开发

'use strict';

var webpackDistConfig = require('./webpack.dist.config.js'),
    webpackDevConfig = require('./webpack.config.js');

var mountFolder = function (connect, dir) {
   return connect.static(require('path').resolve(dir));
};

module.exports = function (grunt) {
  // Let *load-grunt-tasks* require everything
  require('load-grunt-tasks')(grunt);

  // Read configuration from package.json
  var pkgConfig = grunt.file.readJSON('package.json');

  grunt.initConfig({
    pkg: pkgConfig,

    webpack: {
      options: webpackDistConfig,

      dist: {
        cache: false
      }
    },

    'webpack-dev-server': {
      options: {
        hot: true,
        port: 8000,
        webpack: webpackDevConfig,
        publicPath: '/assets/',
        contentBase: {target : 'http://localhost:13800'},
      },

      start: {
        keepAlive: true,
      }
    },

    connect: {
      options: {
        port: 8000,
        keepalive: true,
      },
      proxies: [
        {
          context: '/',
          host: '127.0.0.1',
          port: 8031,
          https: false,
          xforward: false
        }
      ],
      dev: {
        options: {
          port : 13800,
          middleware: function (connect) {
            return [
              mountFolder(connect, pkgConfig.src),
              require('grunt-connect-proxy/lib/utils').proxyRequest
            ];
          }
        }
      },
      dist: {
        options: {
          middleware: function (connect) {
            return [
              mountFolder(connect, pkgConfig.dist),
              require('grunt-connect-proxy/lib/utils').proxyRequest
            ];
          }
        }
      }
    },

    open: {
      options: {
        delay: 500
      },
      dev: {
        path: 'http://localhost:<%= connect.options.port %>/webpack-dev-server/'
      },
      dist: {
        path: 'http://localhost:<%= connect.options.port %>/'
      }
    },

    karma: {
      unit: {
        configFile: 'karma.conf.js'
      }
    },

    copy: {
      dist: {
        files: [
          // includes files within path
          {
            flatten: true,
            expand: true,
            src: ['<%= pkg.src %>/*'],
            dest: '<%= pkg.dist %>/',
            filter: 'isFile'
          },
          {
            flatten: true,
            expand: true,
            src: ['<%= pkg.src %>/styles/*'],
            dest: '<%= pkg.dist %>/styles/'
          },
          {
            flatten: true,
            expand: true,
            src: ['<%= pkg.src %>/images/*'],
            dest: '<%= pkg.dist %>/images/'
          },
        ]
      }
    },

    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            '<%= pkg.dist %>'
          ]
        }]
      }
    }
  });

  grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['configureProxies', 'build', 'open:dist', 'connect:dist']);
    }

    grunt.task.run([
      'open:dev',
      'webpack-dev-server'
    ]);
  });

  grunt.registerTask('content', ['configureProxies', 'connect:dev']);

  grunt.registerTask('test', ['karma']);

  grunt.registerTask('build', ['clean', 'copy', 'webpack']);

  grunt.registerTask('default', []);
};

于 2014-12-27T06:42:34.273 回答