如何在vue里面优雅的解决跨域,路由冲突问题
当我们在路由里面配置成以下代理可以解决跨域问题
proxyTable: {
'/goods/*': {
target: 'http://localhost:3000'
},
'/users/*': {
target: 'http://localhost:3000'
}
},
这种配置方式在一定程度上解决了跨域问题,但是会带来一些问题,
比如我们的vue 路由 也命名为 goods,这时候就会产生了冲突,
如果项目中接口很多,都在这里配置是很麻烦的,也容易产生路由冲突。
正确的姿势
如果把所有的接口,统一规范为一个入口,在一定程度上会解决冲突
把以上配置统一前面加上 /api/
proxyTable: {
'/api/**': {
target: 'http://localhost:3000'
},
},
如果我们配置成这种方式,在使用http请求的时候就会发生变化,会在请求前面加上一个api,相对路由也会发生变化,也会在接口前面加上api,这样也会很麻烦,我们可以使用以下方式来解决这个问题
proxyTable: {
'/api/**': {
target: 'http://localhost:3000',
pathRewrite:{
'^/api':'/'
}
},
},
上面这个代码,就是把咱们虚拟的这个api接口,去掉,此时真正去后端请求的时候,不会加上api这个前缀了,那么这样我们前台http请求的时候,还必须加上api前缀才能匹配到这个代理,代码如下:
logout(){
axios.post('/api/users/logout').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
},
getGoods(){
axios.post('/api/goods/list').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
}
我们可以利用axios的baseUrl直接默认值是 api,这样我们每次访问的时候,自动补上这个api前缀,就不需要我们自己手工在每个接口上面写这个前缀了
在入口文件里面配置如下:
import Axios from ‘axios’
import VueAxios from ‘vue-axios’
Vue.use(VueAxios, Axios)
Axios.defaults.baseURL = ‘api’
如果这配置 ‘api/’ 会默认读取本地的域
上面这样配置的话,不会区分生产和开发环境
在config 文件夹里面新建一个 api.config.js 配置文件
const isPro = Object.is(process.env.NODE_ENV, ‘production’)
module.exports = {
baseUrl: isPro ? ‘http://www.vnshop.cn/api/’ : ‘api/’
}
然后在main.js 里面引入,这样可以保证动态的匹配生产和开发的定义前缀
import apiConfig from ‘…/config/api.config’
import Axios from ‘axios’
import VueAxios from ‘vue-axios’
Vue.use(VueAxios, Axios)
Axios.defaults.baseURL = apiConfig.baseUrl
经过上面配置后,在dom里面可以这样轻松的访问,也不需要在任何组件里面引入axios模块了。
logout(){
this.$http.post('/users/logout').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
},
getGoods(){
this.$http.post('/goods/list').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
}
最终代码
在代理里面配置
proxyTable: {
'/api/**': {
target: 'http://localhost:3000',
pathRewrite:{
'^/api':'/'
}
},
},
在config里面的api.config.js 配置
在config 文件夹里面新建一个 api.config.js 配置文件
const isPro = Object.is(process.env.NODE_ENV, ‘production’)
module.exports = {
baseUrl: isPro ? ‘http://www.vnshop.cn/api/’ : ‘api/’
}
关于生产和开发配置不太了解
可以去 dev-server.js 里面看配置代码
const webpackConfig = (process.env.NODE_ENV === ‘testing’ || process.env.NODE_ENV === ‘production’) ?
require(’./webpack.prod.conf’) :
require(’./webpack.dev.conf’)
在main.js 入口文件里面配置
import apiConfig from ‘…/config/api.config’
import Axios from ‘axios’
import VueAxios from ‘vue-axios’
Vue.use(VueAxios, Axios)
Axios.defaults.baseURL = apiConfig.baseUrl
在dom里面请求api的姿势
logout(){
this.$http.post('/users/logout').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
},
getGoods(){
this.$http.post('/goods/list').then(result=>{
let res = result.data;
this.nickName = '';
console.log(res);
})
}
解决线上跨域:
优雅的在一台vps(云主机)上面部署vue+mongodb+express项目 (vue+express篇)
vue.js vue-cli 前后端分离 跨域 express
发布于 2017-08-22 约 7 分钟
优雅的在一台vps(云主机)上面部署vue+mongodb+express项目 (vue+express篇)
项目: vue + express + mongodb
项目前后分离部署在一台服务器上面
express端口:3000
mongodb端口:27017
vue端口:本地是8080 服务端是:80
本地开发配置
本地开发基于vue cli 端口是 8080如果请求api的时候在前缀加上localhost:3000会提示跨域问题,我们可以使用下面方式来解决这个问题
在vue项目路径找到这个文件 /vue-item/config/index.js 找到这行代码:
proxyTable: {}
添加如下配置
demo:
proxyTable: {
‘/v1/**’:{
target: ‘http://localhost:3000/’,
pathRewrite: {
‘^/v1’: ‘/’
}
}
}
v1 是我给api自动添加的前缀
这个前缀可以使用 axios 配置添加
在main.js 主入口文件添加
如下
import apiConfig from ‘…/config/api.config’
// import axios
import Axios from ‘axios’
import VueAxios from ‘vue-axios’
Vue.use(VueAxios, Axios)
// Axios.defaults.baseURL = apiConfig.baseUrl;
Axios.defaults.baseURL = ‘v1/’ 这样也ok的
api.config
判断是开发模式还是本地模式,其实不需要这么麻烦 直接
const isProdMode = Object.is(process.env.NODE_ENV, ‘production’)
module.exports = {
baseUrl: isProdMode ? ‘api.shudong.wang/v1/’ : ‘v1/’
}
如果把axios 配置了自动前缀
每次访问的时候
data(){
return {
articleList:Object
}
},
mounted: function(){
this.getArticleList()
},
methods:{
getArticleList(){
console.log(111111111)
this.
h
t
t
p
.
g
e
t
(
"
/
a
r
t
i
c
l
e
/
l
i
s
t
"
)
/
/
t
h
i
s
.
http.get("/article/list") // this.
http.get("/article/list")//this.http axios使用的一种方式
.then((response)=>{
console.log(response.data)
let res = response.data;
this.articleList = res.data;
})
.catch((error) =>{
console.log(error)
})
}
},
上面请求的例子中相当于访问: localhost:8080/v1/article/list
这样就可以解决跨域问题
其实最终访问的是 localhost:3000/article/list express的api
这个v1只是api版本的标识,如果想带着,并且api是可以v1版本方式访问的,把代理的路径重新规则去掉就可以
操作如下:
proxyTable: {
'/v1/**':{
target: 'http://localhost:3000/',
//pathRewrite: { //这个规则去掉
// '^/v1': '/'
//}
},
'/goods/*':{
target:'http://localhost:3000'
},
'/users/**':{
target:'http://localhost:3000'
}
}
服务端部署
本地可以使用proxyTable 解决跨域问题,那么服务端怎么解决跨域问题呢?
answer:使用nginx反向代理
nginx配置: 仔细分析一下看看是否适合自己的业务场景
server
{
listen 80;
#listen [::]:80;
server_name zhenfan.shudong.wang ; # 你的域名不需要加http
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/zhenfan/dist;
include none.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location /v1 {
proxy_pass http://127.0.0.1:3000/; # 当访问v1的时候默认转发到 3000端口
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
关于express链接mongodb可以直接填写端口号,不存在跨域问题,直接 127.0.0.1:27017就ok,
怎么在服务器上面搭建可以参考上篇 mongodb篇
关于有什么问题,可以在下面留言,希望你是来讨论技术的。
上次写完一篇,一个小朋友,来到这里咬文嚼字,针对 部署这个词,说用的不当,还口口声声说是来讨论技术,把注意力放在这个上面上真没意义。
希望本篇文章能帮到你,解决你的问题。