Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use middleware with webpack-dev-server? #285

Closed
sergey-lapin opened this issue Oct 5, 2015 · 4 comments
Closed

Can I use middleware with webpack-dev-server? #285

sergey-lapin opened this issue Oct 5, 2015 · 4 comments

Comments

@sergey-lapin
Copy link

I am trying to solve this issue lahmatiy/component-inspector#9 (comment)

I tried

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

var app = new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  historyApiFallback: true,
  stats: {
    colors: true
  }
});

app.use(function(req, res, next) {
  console.log('Middleware triggered');
  next();
});

app.listen(3000, 'localhost', function (err) {
  if (err) {
    console.log(err);
  }

  console.log('Listening at localhost:3000');
});

with no result.
Can middlewares used with webpack-dev-server somehow?

@chemzqm
Copy link

chemzqm commented Oct 6, 2015

The append middleware would not be triggered as the response already send.
According to this [https://github.com/webpack/webpack-dev-server/commit/6e0e95c5f07751546f1e1db439c68e81be0624ae] commit, you can prepend middlewares like this:

var server = new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  historyApiFallback: true,
  setup: function(app) {
    app.use(function(req, res, next) {
      console.log('Middleware triggered');
      next();
    });
  },
  stats: {
    colors: true
  }
});

assign the setup option with a function.

@sergey-lapin
Copy link
Author

Thanks! This works

@geddski
Copy link

geddski commented Dec 2, 2015

This was helpful to me as well, thanks @chemzqm

@noahingh
Copy link

noahingh commented Mar 3, 2019

This was helpful. FYI for someone It was deprecated like below (here)!

This option is deprecated in favor of devServer.before and will be removed in v3.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants