I'm trying to set up a proxy within my webpack dev server. The issue is that I don't control the server I'm connecting to, and I need to authenticate the request.
Is there a way I can add cookies on to the request I send to the proxy server? I've looked through the webpack dev server proxy server page, and the node-http-proxy page it links to, and I don't see any mention of cookies. I'm also not sure if there's a way for me to see these forwarded requests, so I can't tell if anything I'm trying is doing anything.
Any ideas?
webpack-dev-server can be used to quickly develop an application. See the development guide to get started.
The Result Either method will start a server instance and begin listening for connections from localhost on port 8080 . webpack-dev-server is configured by default to support live-reload of files as you edit your assets while the server is running. See the documentation for more use cases and options.
If you need to only rewrite the cookie domain for the proxy, check out the option cookieDomainRewrite in node-http-proxy.
Additionally if you wanted to find a way to inject in custom behavior around cookies on requests / responses, then check out the events you can hook in to:
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target',JSON.stringify(proxyRes.headers, true, 2));
});
proxy.on('proxyReq', function (proxyRes, req, res) {
console.log('RAW Request from the target',JSON.stringify(proxyReq.headers, true, 2));
});
https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
These options can be added to the webpack.config.js for the devServer proxy, like this:
{
devServer: {
proxy: {
onProxyReq: function(proxyReq, req, res){
proxyReq.setHeader('x-added', 'foobar');
},
cookieDomainRewrite: ""
}
}
}
https://github.com/chimurai/http-proxy-middleware#http-proxy-events
After looking into this further, it does seem like the dev server will just forward any cookies you send it. Didn't work for the authentication I was trying to do, I guess Amazon has some more security in place that I couldn't account for, but that is the answer.
Add cookies to the request you're sending to the dev server, and set up the proxy properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With