路由的两种模式:history模式和hash模式
两者的区别
hash模式是路由的默认模式,浏览器标识路径会有/#/标识,不太美观但是兼容性更好,会将文件的路径转换为资源访问,不会发送实际请求。
history模式是路由的另一种模式,浏览器表示路径为/标识,比较美观但是兼容性没有hash优秀,会将文件发送实际请求,会产生404
解决history兼容性方式
1、使用npm中间件
安装:
npm install --save connect-history-api-fallback
// 初始化express
const express = require('express')
const history = require('connect-history-api-fallback');
// 使用express
const app = express()
app.use(history())
app.use(express.static(__dirname + '/static'))
app.get('/person', (req, res) => {
res.send({
name: 'john',
age: 18
})
})
app.listen(5005, (err) => {
if (!err) console.log('服务器已启动!')
})
2、和后端人员配合校验页面路由展示不同数据