The new version react proxy configuration

The new study react

Start cross-domain configuration react when

 

In the online view is packjson.json inside add the following code:

1 "proxy": {
2   "/api": {
3     "target": "http://localhost:8080"
4     },
5 }

 

But after starting the project being given:

 

 Later, look for errors in the Internet and found react new version does not support that setting up a reverse proxy

 The new version of practice

 Install   http-proxy-middleware

1 npm install http-proxy-middleware --save
2 yarn add http-proxy-middleware

 

 Then create setupProxy.js file in the src directory, src / setupProxy.js

 setupProxy.js write

1 module.exports = function (app) {
2     app.use(proxy('/api',{
3         target: 'http://localhost:8080',
4         changeOrigin: true,
5         pathRewrite: {
6             "^/api": "/"
7         }
8     }))
9 }

 

 

 

Guess you like

Origin www.cnblogs.com/dropInInt/p/11842147.html