前言
如果你有单独的后端开发服务器 api,并且希望在同域名下发送 api 请求 ,那么代理某些 url 会很有用。
解决开发环境的跨域问题(不用在去配置nginx和host, 爽歪歪~~)
在webpack.config.js中配置
下面简单介绍一下五个经常使用的场景
使用一:
mmodule.exports = { //... devserver: { proxy: { '/api': 'http://localhost:3000' } } };
请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/api/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/api/user
使用二
如果你想要代码多个路径代理到同一个target下, 你可以使用由一个或多个「具有 context 属性的对象」构成的数组:
module.exports = { //... devserver: { proxy: [{ context: ['/auth', '/api'], target: 'http://localhost:3000', }] } };
使用三:
如果你不想始终传递 /api ,则需要重写路径:
module.exports = { //... devserver: { proxy: { '/api': { target: 'http://localhost:3000', pathrewrite: {'^/api' : ''} } } } };
请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/user
使用四:
默认情况下,不接受运行在 https 上,且使用了无效证书的后端服务器。如果你想要接受,只要设置 secure: false 就行。修改配置如下:
module.exports = { //... devserver: { proxy: { '/api': { target: 'https://other-server.example.com', secure: false } } } };
使用五:
有时你不想代理所有的请求。可以基于一个函数的返回值绕过代理。
在函数中你可以访问请求体、响应体和代理选项。必须返回 false 或路径,来跳过代理请求。
例如:对于浏览器请求,你想要提供一个 html 页面,但是对于 api 请求则保持代理。你可以这样做:
module.exports = { //... devserver: { proxy: { '/api': { target: 'http://localhost:3000', bypass: function(req, res, proxyoptions) { if (req.headers.accept.indexof('html') !== -1) { console.log('skipping proxy for browser request.'); return '/'; } } } } } };
解决跨域原理
上面的参数列表中有一个changeorigin参数, 是一个布尔值, 设置为true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求,
module.exports = { //... devserver: { proxy: { '/api': { target: 'http://localhost:3000', changeorigin: true, } } } };
vue-cli中proxytable配置接口地址代理示例
修改 config/index.js
module.exports = { dev: { // 静态资源文件夹 assetssubdirectory: 'static', // 发布路径 assetspublicpath: '/', // 代理配置表,在这里可以配置特定的请求代理到对应的api接口 // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html proxytable: { // 例如将'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx' '/api': { target: 'https://wangyaxing.cn', // 接口的域名 secure: false, // 如果是https接口,需要配置这个参数 changeorigin: true, // 如果接口跨域,需要进行这个参数配置 }, // 例如将'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx' '/img': { target: 'https://cdn.wangyaxing.cn', // 接口的域名 secure: false, // 如果是https接口,需要配置这个参数 changeorigin: true, // 如果接口跨域,需要进行这个参数配置 pathrewrite: {'^/img': ''} // pathrewrite 来重写地址,将前缀 '/api' 转为 '/'。 } }, // various dev server settings host: 'localhost', // can be overwritten by process.env.host port: 4200, // can be overwritten by process.env.port, if port is in use, a free one will be determined }
更多参数
dev-server 使用了非常强大的, http-proxy-middleware 基于 http-proxy 实现的,可以查看 http-proxy 的源码和文档:。
参考
api proxying during development
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
androidx+viewpage+tablayout+json开发(加载图片和视频)
网友评论