location / { try_files $uri $uri/ /index.html; } location /api{ proxy_pass http://127.0.0.1:xxxx; //xxx为指定端口号 proxy_http_version 1.1; proxy_read_timeout 3600s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Host $host; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /socket{ proxy_pass http://127.0.0.1:xxxx; //xxx为指定端口号 proxy_http_version 1.1; proxy_read_timeout 3600s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Host $host; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
module.exports = { // publicPath: ‘./‘, //publicPath取代了baseUrl outputDir: ‘dist‘, lintOnSave: true, //eslint-loader在保存的时候进行检查 runtimeCompiler: true, //关键点在这 原来的 Compiler 换成了 runtimeCompiler // 调整内部的 webpack 配置。 chainWebpack: () => {}, configureWebpack: () => {}, // 配置 webpack-dev-server 行为。 devServer: { compress: true, //进行压缩代码 // open: process.platform === ‘darwin‘, // open: true, //运行npm run serve完成后自动打开浏览器 // port: 80, hot: true, proxy: { ‘/socket.io‘: { target: ‘https://域名/socket‘, // target: ‘http://192.168.31.129:8765‘, // target: ‘http://192.168.0.135:8765‘, changeOrigin: true, // needed for virtual hosted sites logLevel: ‘debug‘, ws: true }, ‘sockjs-node‘: { target: ‘https://域名/socket‘, // target: ‘http://192.168.31.129:8765‘, // target: ‘http://192.168.0.135:8765‘, ws: false, changeOrigin: true }, ‘/api‘: { target: ‘https://域名/api‘, // target: ‘http://192.168.31.129:8888‘, // target: ‘http://192.168.0.135:8888‘, changeOrigin: true, ws: true, pathRewrite: { ‘^/api‘: ‘‘ } }, } }, }
import Vue from ‘vue‘ import App from ‘./App.vue‘ import router from ‘./router/router.js‘ import axios from ‘axios‘ import Cookies from ‘vue-cookies‘ import store from ‘./store/store.js‘ Vue.prototype.$axios= axios axios.defaults.withCredentials = true //引入vant import Vant from ‘vant‘; import ‘vant/lib/index.css‘; // import { writeHeapSnapshot } from ‘v8‘ Vue.use(Vant).use(Cookies); //引入socketIO import SocketIO from ‘socket.io-client‘; import VueSocketIO from ‘vue-socket.io‘; Vue.use(new VueSocketIO({ debug: false, connection: SocketIO(‘https://域名.com‘) // connection: SocketIO(‘http://192.168.31.129:8080‘) // connection: SocketIO(‘http://192.168.0.135:8080‘) })); Vue.config.productionTip = false // 路由判断登录 根据路由配置文件的参数 router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requireAuth)){ // 判断该路由是否需要登录权限 if (Cookies.get(‘comedy‘)) { // 判断当前的token是否存在 ; 登录存入的token next(); } else { next({ path: ‘/login‘, query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由 }) } } else { next(); } }); new Vue({ VueSocketIO, router, store, render: h => h(App), }).$mount(‘#app‘)
import axios from ‘axios‘ import Vue from ‘vue‘; import { Toast } from ‘vant‘; Vue.use(Toast); let loadingInstance = null // const ConfigBaseURL = `https://域名/api` // export const ConfigBaseURL = `http://192.168.31.129:8888` // export const ConfigBaseURL = `https://comedy.ilovenana.com/api` export const ConfigBaseURL = `/api` //使用create方法创建axios实例 const Service = axios.create({ timeout:5000, //设置超时时间 baseURL:ConfigBaseURL, method:‘get‘, hearders:{ ‘Content-Type‘:‘application/json;charset=UTF-8‘ } }) //添加请求拦截器 Service.interceptors.request.use( config=>{ console.log("请求中"); loadingInstance = Toast.loading({ duration:0, message: ‘加载中...‘, forbidClick: true, }); return config },error=>{ Promise.reject(error); } ) // 添加响应拦截器 Service.interceptors.response.use( response => { console.log("响应成功"); loadingInstance.close() return response }, error => { console.log( error) console.log("响应失败"); loadingInstance.close() return Promise.reject(error) }) export default Service;
import Vue from ‘vue‘ import VueRouter from ‘vue-router‘ Vue.use(VueRouter) const routes=[ { path:‘/‘, name:"Home", meta: { requireAuth: true, }, component:()=>import(‘../views/Index.vue‘) }, { path:‘/login‘, name:"Login", component:()=>import(‘../views/Login.vue‘) }, ... ] const router = new VueRouter({ mode:‘history‘, routes }) export default router
原文:https://www.cnblogs.com/comedy/p/14729863.html