Created
January 7, 2021 12:33
-
-
Save ravi123shanker/f054bf0d5f6f7d3db772858aebf109d7 to your computer and use it in GitHub Desktop.
How to Configure a Webpack Proxy for HTTPS domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 1: | |
Add below line in your start script command in package.json | |
"start": "webpack-dev-server --https --env.apiBaseUrl='https://yourdomain.com/' --config webpack.config.js" | |
Step 2: | |
After above changes, change your webpack.config.js as below: | |
module.exports = function({ apiBaseUrl = "https://yourdomain.com/" }) { | |
return { | |
... | |
devServer: { | |
... | |
proxy: { | |
"/api": { | |
"changeOrigin": true, | |
"cookieDomainRewrite": "localhost", | |
"target": apiBaseUrl, | |
onProxyReq: proxyReq => { | |
// Browers may send Origin headers even with same-origin | |
// requests. To prevent CORS issues, we have to change | |
// the Origin to match the target URL. | |
if (proxyReq.getHeader('origin')) { | |
proxyReq.setHeader('origin', apiBaseUrl); | |
} | |
} | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment