访问webpack-middleware中的中间件数组

时间:2017-11-28 14:58:38

标签: webpack middleware html-webpack-plugin webpack-dev-middleware

我们有使用grunt编写的lecasy应用程序。它还使用了这个东西https://github.com/gruntjs/grunt-contrib-connect,这使我们可以使用中间件数组,这样我们就可以像这样添加或取消中间件:

grunt.initConfig({
  connect: {
    server: {
      options: {
        middleware: function(connect, options, middlewares) {
          // inject a custom middleware into the array of default middlewares
          middlewares.unshift(function(req, res, next) {
            if (req.url !== '/hello/world') return next();

            res.end('Hello, world from port #' + options.port + '!');
          });

          return middlewares;
        },
      },
    },
  },
});

现在我们将项目迁移到webpack并尝试使用webpack实现相同的行为:

var server = new WebpackDevServer(webpack(config), {
    //publicPath: config.output.publicPath,

    hot: true,
    port: 32728,
    // historyApiFallback: true,
    setup: function(app) {
        console.dir(app);
      app.use(function(req, res, next) {
          // how to access array of middlewares here ?
      } 
    },
    stats: {
      colors: true
    }
  });
  server.listen(32728);

0 个答案:

没有答案