vue-router的History 模式常用的三种配置方式(去掉地址栏中的#号)
第一种:nginx配置
conf主要的配置代码:
http {
# nginx负载均衡配置
upstream dynamic_balance {
#ip_hash;
server 192.168.100.123: 8081;
}
# 省略其他
server {
listen 80;
server_name localhost;
#访问工程路径
root website;
index index.html index.htm;
#转发把原http请求的Header中的Host字段也放到转发的请求
proxy_set_header Host $host;
#获取用户真实IP
proxy_set_header X - real - ip $remote_addr;
proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
#接口转发
location /base/ {
proxy_pass http: //dynamic_balance;
}
#启用history模式( 什么请求都只返回index.html)
location / {
try_files $uri $uri / /index.html;
}
}
}
第二种:原生node.js
const http = require('http');
const fs = require('fs');
const path = require('path');
const httpProxy = require('http-proxy');
const childProcess = require('child_process'); // 可自动打开浏览器
const filepath = path.resolve(__dirname,'../');
const proxy = httpProxy.createProxyServer(); // 创建代理服务器
const {proxyTable = [],port = 8080} = require('./index.js');
http.createServer(function(req,res){
fs.readFile(filepath + req.url,function(err,data) {
proxyTable.forEach((item) => {
if(req.url.indexOf(item.api) !== -1) { // 匹配上接口代理
proxy.web(req,res,{target: item.target});
proxy.on('error',function(e) { // 代理失败处理
console.log(e);
})
} else {
if(err) {
fs.readFile(filepath + '/index.html', 'utf-8',(err,data) => {
res.write(data);
res.end()
})
} else {
res.write(data);
res.end();
}
}
})
})
}).listen(port,() => {
console.log('服务启动了');
});
childProcess.exec(`start http://localhost:${port}/`);
这儿用到了接口代理,需要安装http-proxy:npm i http-proxy -D。
其中引入的index.js代码如下:
module.exports = {
port: 8081,
host: 'localhost',
proxyTable: [{
api: '/webgate',
target: 'http://192.168.100.112:8080/'
}]
}
第三种:基于 Node.js 的 Express的connect-history-api-fallback 中间件
const history = require('connect-history-api-fallback');
const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const childProcess = require('child_process');
const {proxyTable = [],port = 8080,host = 'localhost'} = require('./index.js');
const pathname = path.resolve(__dirname, '../');
app.use('/',history({
index: `/console.html`,
verbose: true
}));
app.use('/',express.static(`${pathname}`)); // 设置静态资源访问路径
proxyTable.forEach((item) => {
app.use(createProxyMiddleware(item.api,{
target: item.target,
changeOrigin: true,
ws: true
}));
})
app.listen(port,() => {
console.log(`listen:${port}`);
})
childProcess.exec(`start http://${host}:${port}`)
其中引入了的index.js跟第二种代码一样。
参考地址:《HTML5 History 模式》
vue-router的History 模式常用的三种配置方式(去掉地址栏中的#号)的更多相关文章
- IIS下PHP的三种配置方式比较
在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...
- 【jdbc】【c3p0】c3p0三种配置方式【整理】
c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...
- tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- 【转】tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- 【c3p0】 C3P0的三种配置方式以及基本配置项详解
数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
- Hive metastore三种配置方式
http://blog.csdn.net/reesun/article/details/8556078 Hive的meta数据支持以下三种存储方式,其中两种属于本地存储,一种为远端存储.远端存储比较适 ...
- c3p0三种配置方式(automaticTestTable)
c3p0的配置方式分为三种,分别是http://my.oschina.net/lyzg/blog/551331.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文 ...
- MyEclipse中web服务器的三种配置方式
初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通 ...
- selenium常用的三种等待方式
一.强制等待 使用方法:sleep(X),等待X秒后,进行下一步操作. 第一种也是使用最简单的一种办法就是强制等待sleep(X),强制让浏览器等待X秒,不管当前操作是否完成,是否可以进行下一步操作, ...
随机推荐
- FPGA bit转bin文件
首先科普一下 什么是bitstream文件 FPGA比特流(bitstream)是一种用于配置可编程逻辑器件的数据格式,特别是现场可编程门阵列(FPGA).比特流包含了硬件逻辑电路.路由信息以及芯片上 ...
- linux信号机制(初识版)
转载 https://www.zhihu.com/question/24913599/answer/2584544572 信号是操作系统内核为我们提供用于在进程间通信的机制,内核可以利用信号来通知进程 ...
- npm私服 verdaccio 搭建
1.什么是npm 私服 我们前端(web,nodejs)平常使用的各种包,什么vue,react,react-router, zustand等,都会从 https://registry.npmjs.o ...
- Vue 基于VSCode结合Vetur+ESlint+Prettier统一Vue代码风格
基于VSCode结合Vetur+ESlint+Prettier统一Vue代码风格 插件安装 安装Vetur,ESlint, Prettier - Code formatter插件 安装方法(安装ESl ...
- Linux系统下查找安装包所在目录
Linux系统下查找安装包所在目录 想知道Linux系统下安装了哪些软件包,以及软件包安装在哪个目录下,可以用以下命令 1. which which命令查找出相关命令是否已经在搜索路径中,例子如下:$ ...
- docker 安装 centos8 mysql8 java tomcat
docker 安装 centos8 mysql8 java tomcat 一,首先在window10系统安装docker,这里就不再描述了. 二,启动docker下载安装centos8镜像 注意: ...
- python高性能计算:cython使用openmp并行 —— 报错:undefined symbol: omp_get_thread_num
test.pyx文件: from cython.parallel cimport parallel from openmp cimport omp_get_thread_num cpdef void ...
- 循环神经网络 —— LSTM 图片
- 《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第三章
图3.1 import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np mpl.rcParams['font. ...
- PEP 703作者给出的一种no-GIL的实现——python3.9的nogil版本
PEP 703的内容是什么,意义又是什么呢? 可以说python的官方接受的no-GIL提议的PEP就是PEP 703给出的,如果GIL帧的从python中移除那么可以说对整个python生态圈将有着 ...