您现在的位置是:首页 > 编程 > 

使用 express 处理前端路由的 history 模式问题

2025-07-28 01:21:55
使用 express 处理前端路由的 history 模式问题 主要使用的中间件:bripkens/connect-history-api-fallback: Fallback to index.html for applicati that are using the HTML 5 history APIserve.js代码语言:javascript代码运行次数:0运行复制ct ex

使用 express 处理前端路由的 history 模式问题

主要使用的中间件:

bripkens/connect-history-api-fallback: Fallback to index.html for applicati that are using the HTML 5 history API

serve.js

代码语言:javascript代码运行次数:0运行复制
ct express = require("express");ct history = require("connect-history-api-fallback");
ct app = express();
// 先定义后端路由app.get("/health", function (req, res) {  res.send("ok");});
// 再使用 history 中间件app.use(history());
// 最后配置 静态资源文件app.use(express.static(__dirname + "/public"));
// 监听端口号app.listen(8081, () => {  cole.log("服务已启动");});

执行:node serve.js

PS 需要前端处理路由不匹配问题(404)

在 koa 中使用这个中间件

上面这个中间件是基于 express 的,在 koa/egg.js 中使用,可以借助 koa-connect 这个库。

详见:每天一个npm包:koa-connect - 知乎

GitHub - vkurchatkin/koa-connect: Use connect and express middleware in koa

参考

教你怎么快速搭建一个 ode + Express 静态服务器 - 简书

Express 路由

HTML5 History 模式 | Vue Router


原文链接:

本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2021年12月17日,如有侵权请联系 cloudcommunity@tencent 删除expreistory路由前端中间件

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1222211.html

相关标签:无
上传时间: 2025-07-25 18:20:02
留言与评论(共有 8 条评论)
本站网友 塞内加尔玫瑰湖
10分钟前 发表
() => { cole.log("服务已启动");});执行:node serve.js PS 需要前端处理路由不匹配问题(404) 在 koa 中使用这个中间件上面这个中间件是基于 express 的
本站网友 香蕉和枣
0秒前 发表
res) { res.send("ok");}); // 再使用 history 中间件app.use(history()); // 最后配置 静态资源文件app.use(express.static(__dirname + "/public")); // 监听端口号app.listen(8081
本站网友 win7动态壁纸
16分钟前 发表
Fallback to index.html for applicati that are using the HTML 5 history APIserve.js代码语言:javascript代码运行次数:0运行复制ct express = require("express");ct history = require("connect-history-api-fallback"); ct app = express(); // 先定义后端路由app.get("/health"
本站网友 白果怎么吃
16分钟前 发表
分享自作者个人站点/博客
本站网友 丽江故事
13分钟前 发表
但未经作者同意必须保留此段声明
本站网友 山西建邦
21分钟前 发表
本文参与 腾讯云自媒体同步曝光计划
本站网友 产权年限是什么意思
28分钟前 发表
如有侵权请联系 cloudcommunity@tencent 删除前往查看expreistory路由前端中间件