How can axios request the analog interface of mock Server when setting up a proxy with webpack-server-dev 's proxy?

problem description

the newcomer has just come into contact with a question about the cross-domain resolution of webpack-server-dev.
has already configured the domain name of the proxy, but axios directly requests the domain name + route after the proxy, and naturally returns 404, because there is no mockServer interface requested at all. Maybe I have some misunderstanding about this. I hope there is a great god to explain

the environmental background of the problems and what methods you have tried

for example, the server my mock server listens to is the 8009 port, while I direct the domain name of the interface to the project port 8008 . 404 is displayed when requesting with axios , because this interface does not exist at all. Do you want to use fetch ?

related codes

mock Server Services: localhost: 8009

//webpack.config.js
module.exports = {
    ...
    
    devServer: {
        proxy: {
            "/api": "http://localhost:8008",
        }
    },
    
    ...
}
//
axios.get("v1/api/tablelist")
.then(response => {
    console.log("response =========>", response)
})

what result do you expect? What is the error message actually seen?

without adding a request header, you can not only solve cross-domain problems, but also successfully request data. Thank you

_

Update question: 2018.11.20

may have misunderstood proxy before. First of all, my background interface address is http://localhost:8009/api/tablelist
, while the address of my project is http://localhost:8008
, so the address that needs to be proxied should be 8009 , right?
that is how to configure devServer.

.
...
devServer: {
    proxy: {
        "/api": "http://localhost:8009",
    }
},
...

I don"t know if this understanding is correct. In addition, I set it like this, and the request interface was changed to

.
axios.get("/api/tablelist")
.then(response => {
    console.log("response =========>", response)
})

but why is the request url in network still the 8008 port? Haven"t you already proxied to 8009 ? Thank you

_ _

Update question: 2018.11.21

access to servers and interfaces can get interface data.

clipboard.png

. networkurl http://localhost:8008/xxx

clipboard.png

Feb.27,2022

network is definitely showing 8008, and the agent is on the backend.


your rule says / api, but you actually start with / v1. Of course it's impossible to match.


when the pathname of the matching api url in your devServer configuration begins with / api, the request will be forwarded to the address you specified

for example, axios ({url:'/ api/123'}) in your api will be forwarded to http://localhost:8008/api/123


you need to use pathRewrite, to replace the API field on the match with an empty character, otherwise the matching API field will be automatically added to the target in the target.
in addition, if a single slash match is used, then a single slash match must be placed at the end of the proxy, otherwise the matching API behind the single slash will not be recognized, and they will only recognize the single slash and stop.
here is my example,

proxy: {
            '/myblog/get_articles': {
                target: 'http://localhost:9000/myblog/get_articles',
                secure: false,
                pathRewrite: {'^/myblog/get_articles': ''},
                changeOrigin: true
            },
            '/': {
                target: 'http://localhost:9999/dist/index.html',
                secure: false,
                pathRewrite: {'^/': ''},
            }
        }
Menu