Proxy requests to a separate backend server with vue-cli

后端 未结 3 1137
轮回少年
轮回少年 2021-01-30 09:32

I am using vue-cli webpack-simple template to generate my projects, and I\'d like to proxy requests to a separate, backend server. How can this be easi

3条回答
  •  一整个雨季
    2021-01-30 09:35

    In @vue/cli 3.x:

    • Create a vue.config.js file in the root folder of your project, if you don't already have one.
    • Have its contents as follows:
    // vue.config.js
    module.exports = {
      devServer: {
        proxy: {
          "/gists": {
            target: "https://api.github.com",
            secure: false
          }
        }
      }
    };
    

    Now any call to (assuming your dev server is at localhost:8080) http://localhost:8080/gists will be redirected to https://api.github.com/gists.


    Another example: proxying all calls

    Say you have a local backend server that is typically deployed at localhost:5000 and you want to redirect all calls to /api/anything to it. Use:

    // vue.config.js
    module.exports = {
        devServer: {
            proxy: {
                "/api/*": {
                    target: "http://localhost:5000",
                    secure: false
                }
            }
        }
    };
    

提交回复
热议问题