Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use webpackproxy

Shulou Source: shulou.com Published: 2022-06-02 08:39:33 09月18日 Update

This article mainly shows you "how to use webpackproxy", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use webpackproxy" this article.

Why use an agent?

Cross-domain

In the process of development, our development environment is generally http:// localhost, but if the requested data is not local, then we need to face the problem of cross-domain request. As we all know, because of the browser's security protocol, we can not directly make cross-domain requests. Proxy is to solve this problem, of course, you can also use jsonp and nginx reverse proxy.

How to act as an agent

Webpack configuration

Here I default to the configuration of the development environment

Find the webpack.config.js file and add the following code under the devServer object

Proxy: {'/ index': {/ / this is the position you want to replace / * * for example, if you want to replace http://localhost:8080/index/xxx with http://10.20.30.120:8080/sth/xxx * then you need to replace the value in front of index, or replace the root address, * you may find that index also needs to be replaced, yes, I will deal with it in the follow-up operation. * / target: 'http://10.20.30.120:8080'// this is the replaced destination address changeOrigin: true / / defaults to false If you need an agent, you need to change it to true pathRewrite: {'^ / index':'/'/ / where http://localhost:8080/index/xxx has been replaced with http://10.20.30.120:8080/} / / then the address in the js file where you made the request needs to ignore http://10.20.30.120:8080/// such as demo.jsaxios.post ({url:'http: / / 10.20.30.120:8080/sth/xxx'// needs to be replaced with the following address url:'/sth/xxx'})

Principle

As a matter of fact, the agent is done using the plug-in http-proxy-middleware. If you are interested, you can search it. I won't explain it here.

The above is all the contents of the article "how to use webpackproxy". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Agent address content article development file environment problem learning help configuration security well-known code location interest principle actual actually object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Linux MySQL Huawei