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

Added Proxy Options #366

Closed
wants to merge 2 commits into from
Closed

Added Proxy Options #366

wants to merge 2 commits into from

Conversation

vmaudgalya
Copy link

// Sample usage in webpack.config.js file
module.exports = merge(common, {
    devtool: 'eval-source-map',
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,

      proxy: {
        "/first/api/*": "http://localhost:8100",
        "/second/api/*" : "http://localhost:8200",
        "/third/api/*" : "http://localhost:8300",
        "/fourth/api/*" : "http://localhost:8400",
        "/fifth/api/*" : "http://localhost:8500"
      },
      onProxyReq : function (proxyReq, req, res) {
        if (req.path.indexOf("/first/api") === 0) {
          console.log("Called first API!");
        }
        Object.keys(proxyHeaders).forEach(function (header) {
          proxyReq.setHeader(header, proxyHeaders[header]);
        });
      }
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin()
    ]
  });

Allows for listening to the onProxyReq event and performing actions accordingly (in this case setting headers)

@vmaudgalya
Copy link
Author

I am currently using the bundled files, please let me know if/when you approve of the proxy options and I'll clean up this commit and revert commit 3027379

@jamsesso
Copy link
Contributor

This ability was added in #359 (merged) along with onProxyRes and others.

@vmaudgalya
Copy link
Author

@jamsesso Thanks for letting me know! If you don't mind, could you please provide me with an example of it being used with the use case I have above (setting headers when a particular API is called)?

More specifically, how would I do this?

proxy: {
  "/first/api/*": "http://localhost:8100",
  "/second/api/*" : "http://localhost:8200",
  "/third/api/*" : "http://localhost:8300",
  "/fourth/api/*" : "http://localhost:8400",
  "/fifth/api/*" : "http://localhost:8500"
 },
 onProxyReq : function (proxyReq, req, res) {
   if (req.path.indexOf("/first/api") === 0) {
     console.log("Called first API!");
   }
   Object.keys(proxyHeaders).forEach(function (header) {
     proxyReq.setHeader(header, proxyHeaders[header]);
   });
 }

@vmaudgalya
Copy link
Author

I figured it out: http://webpack.github.io/docs/webpack-dev-server.html
If anyone finds this, I used

{
    devServer: {
        proxy: {
            '/some/path*': {
                target: 'https://other-server.example.com',
                secure: false,
                bypass: function(req, res, proxyOptions) {
                    if (req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request.');
                        return '/index.html';
                    }
                },
            },
        },
    },
}

from the documentation and modified the headers I was sending out.

@vmaudgalya vmaudgalya closed this Jan 13, 2016
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

Successfully merging this pull request may close these issues.

2 participants