Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use webpackproxy

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report