spa单页面应用history模式后端配置
2 点赞
0 条评论
2445 次浏览
发布于 2021-08-20 20:56
现代化的 web 应用中多数采用 SPA(Single Page Application)的方式来编写网站或者应用,当你的 SPA 的路由方式使用的是 html5 的 history 模式的时候,需要服务器端做相应的配置。
nginx 配置如下:
location / {
try_files $uri $uri/ /index.html;
}
Apache 配置如下:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
原生 Node.js
const http = require('http')
const fs = require('fs')
const httpPort = 80
http.createServer((req, res) => {
fs.readFile('index.html', 'utf-8', (err, content) => {
if (err) {
console.log('We cannot open "index.html" file.')
}
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
})
res.end(content)
})
}).listen(httpPort, () => {
console.log('Server listening on: http://localhost:%s', httpPort)
})
基于 Node.js 的 Express
对于 Node.js/Express,请考虑使用 connect-history-api-fallback 中间件
"赞助我们,我们才能做的更多&更好"
赞助支持
还没有评论
写下你的评论...
最热文章
使用dart-sass替换node-sass
5192 浏览 · 0 评论
浏览器后退跳转到指定的页面
3866 浏览 · 0 评论
好玩的CSS3(3)-- 翻转(旋转正反两张)图片
3860 浏览 · 0 评论
Flutter适配夜间模式
3611 浏览 · 0 评论
pc端常用电脑屏幕分辨率尺寸适配
3570 浏览 · 0 评论
最新文章
js通过扫码枪快速录入的实现
406 浏览 · 0 评论
使用 WebSocket 实现你画我猜实时绘图
745 浏览 · 0 评论
基于code-server部署自己的云端vscode
2871 浏览 · 0 评论
支付宝动态收款码生成自定义金额及备注
2863 浏览 · 0 评论
使用 nohup 命令将程序挂载在后台执行
1959 浏览 · 0 评论
基于OpenLayers实现离线地图
2802 浏览 · 0 评论