简体   繁体   English

package.json中包含的代理不起作用

[英]Proxy included in package.json not working

I have a VUE application that has a net.core backend. 我有一个带有net.core后端的VUE应用程序。

To avoid CORS problems, I would like to use a proxy in all calls. 为了避免CORS问题,我想在所有通话中使用代理。 However, my attemps didn't work, since there is no way that my calls get proxied. 但是,由于没有办法代理我的电话,所以我的尝试没有成功。

Application was made with vue CLI 3, and use typescript. 应用是使用vue CLI 3进行的,并使用了打字稿。

I try to add to package.json the next lines, but the proxy still is not working. 我尝试将以下行添加到package.json中,但是代理仍然无法正常工作。 Every call is made to the same server, and not get proxied. 每次调用均指向同一服务器,并且不会被代理。

"proxyTable": {
    "/api":{
      "target": "http://localhost:5000",
      "changeOrigin": true,
      "pathRewrite": {
        "^/api": ""
      }
    }
  },

"proxy": {
    "/api":"http://localhost:5000"
      }

No one of those lines change the calling port. 这些线路中没有一个改变主叫端口。

Axios call are for example: Axios调用例如:

Axios.post(process.env.VUE_APP_BASE_URI + 'me', { }, {withCredentials: true})

Where the constant is define like: 常量定义如下:

VUE_APP_BASE_URI=api/

The constant work and I can use it. 不断的工作,我可以使用它。 Without the constant, the error is the same. 如果没有该常数,则误差是相同的。

Is there any problem in the way I had write the proxy, or is there something else? 我编写代理的方式是否有问题,还是还有其他问题?

my package.json: 我的package.json:

{
  "name": "xxxxxx",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "proxyTable": {
    "/api":{
      "target": "http://localhost:5000",
      "changeOrigin": true,
      "pathRewrite": {
        "^/api": ""
      }
    }
  },
  "dependencies": {
    "axios": "^0.18.0",
    "bootstrap-vue": "^2.0.0-rc.11",
    "font-awesome": "^4.7.0",
    "register-service-worker": "^1.0.0",
    "vue": "^2.5.17",
    "vue-class-component": "^6.0.0",
    "vue-property-decorator": "^7.0.0",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.0",
    "@vue/cli-plugin-pwa": "^3.0.0",
    "@vue/cli-plugin-typescript": "^3.0.0",
    "@vue/cli-service": "^3.0.0",
    "typescript": "^3.0.0",
    "vue-template-compiler": "^2.5.17"
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

One of the attemps was using the recomendations in this page and no luck. 尝试之一是使用此页面中的建议, 没有运气。

I also readed: 我也读过:

Proxy in package.json not affecting fetch request package.json中的代理不影响获取请求

But I have a react app where it works. 但是我有一个反应应用程序在哪里工作。 But not in this VUE one. 但不是在这个VUE中。

Edit 编辑

According to an answer, I added this file: 根据一个答案,我添加了这个文件:

// vue.config.js
export const devServer = {
    proxy: {
        '/app': {
            target: 'http://localhost:5000/app/',
            ws: true,
            changeOrigin: true
        },
        '/api': {
            target: 'http://localhost:5000/app/api/',
            ws: true,
            changeOrigin: true
        }
    }
};

Still, no proxy is working. 不过,没有代理在工作。

This doesn't work either: 这也不起作用:

  module.exports = {
    devServer: {
      proxy: 'http://localhost:5000/app/'
    }
  }

When using Vue CLI tools you don't set proxy info in package.json file, but in vue.config.js file. 使用Vue CLI工具时,不会在package.json文件中设置代理信息,而是在vue.config.js文件中设置代理信息。 There is docs about it here https://cli.vuejs.org/config/#devserver-proxy 这里有关于它的文档https://cli.vuejs.org/config/#devserver-proxy

If you don't have vue.config.js file, just create it 如果您没有vue.config.js文件,只需创建它

This is the correct syntax 这是正确的语法

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://4.3.2.1:8765',
        ws: true,
        changeOrigin: true,
      },
    },
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM