第一种: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 模式常用的三种配置方式(去掉地址栏中的#号)的更多相关文章

  1. IIS下PHP的三种配置方式比较

    在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...

  2. 【jdbc】【c3p0】c3p0三种配置方式【整理】

    c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...

  3. tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  4. 【转】tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  5. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  6. 【c3p0】 C3P0的三种配置方式以及基本配置项详解

    数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...

  7. Hive metastore三种配置方式

    http://blog.csdn.net/reesun/article/details/8556078 Hive的meta数据支持以下三种存储方式,其中两种属于本地存储,一种为远端存储.远端存储比较适 ...

  8. c3p0三种配置方式(automaticTestTable)

    c3p0的配置方式分为三种,分别是http://my.oschina.net/lyzg/blog/551331.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文 ...

  9. MyEclipse中web服务器的三种配置方式

    初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通 ...

  10. selenium常用的三种等待方式

    一.强制等待 使用方法:sleep(X),等待X秒后,进行下一步操作. 第一种也是使用最简单的一种办法就是强制等待sleep(X),强制让浏览器等待X秒,不管当前操作是否完成,是否可以进行下一步操作, ...

随机推荐

  1. axios和AJAX的区别

    axios和ajax的区别 Axios和Ajax都是用于处理网络请求和与服务器进行通信的技术,但它们之间存在一些关键的区别:12 一.技术基础:Ajax(Asynchronous JavaScript ...

  2. 2 手机号登录时,调的不是login接口,而是注册的一个接口

    手机号登录时,调的不是login接口,而是注册的一个接口

  3. CentOS之yum安装JDK

    1.查看云端目前支持安装的jdk版本 [root@localhost ~]# yum search java|grep jdk ldapjdk-javadoc.noarch : Javadoc for ...

  4. oeasy教您玩转vim - 58 - # 块可视化

    ​ 块可视化编辑 回忆上节课内容 上次我们了解到行可视模式 行可视模式 V 也可配合各种motion o切换首尾 选区的开头和结尾是mark标记 开头是 '< 结尾是 '> 可以在选区内进 ...

  5. ComfyUI进阶:Comfyroll插件 (六)

    ComfyUI进阶:Comfyroll插件 (六) 前言: 学习ComfyUI是一场持久战,而Comfyroll 是一款功能强大的自定义节点集合,专为 ComfyUI 用户打造,旨在提供更加丰富和专业 ...

  6. 解密prompt系列34. RLHF之训练另辟蹊径:循序渐进 & 青出于蓝

    前几章我们讨论了RLHF的样本构建优化和训练策略优化,这一章我们讨论两种不同的RL训练方案,分别是基于过程训练,和使用弱Teacher来监督强Student 循序渐进:PRM & ORM So ...

  7. UE-自带的HotUpdate【转】

    原文链接:https://baijiahao.baidu.com/s?id=1745200406976270792&wfr=spider&for=pc 这是百度可以直接搜索到的 UE4 ...

  8. 【Vue】13 VueRouter Part3 路由守卫

    单页应用中,只存在一个HTML文件,网页的标签,是通过title标签显示的,我们在单页应用中如何修改? JS操作: window.document.title = "标签名称" 也 ...

  9. 强化学习:经典测试环境Cart-pole的原始文献

    参考文献格式: A. G. Barto, R. S. Sutton, and C. W. Anderson. Neuronlike adaptive elements that can solve d ...

  10. 永恒的T800 —— 终结者T800 —— 智能机器人(双足机器人、人形机器人、humanoid)

    终结者T800全身像墨生青铜雕像摆件工艺品艺术品铸铜收藏品铜手办 网店地址: https://item.taobao.com/item.htm?id=745037184577&skuId=52 ...