下面列出几个发现的问题:
- vite.config.ts在生产环境需要配置
base
路径字段
export default defineConfig({
base: './',
//...
})
-
前后端路径一定要做区分,比如后端路径加上
/api/
前缀 本项目大部分都做了区分,但是,经调试,ws 链接路径没加/api/
前缀,导致连接出错 -
ip 硬编码问题,经排查,ws url 使用硬编码 ip 和协议名,导致连接出错
const socketURL = `ws://10.34.38.118/chat/${username}/${touxiang}`;
应该改为
var socketURL = new URL(`/api/chat/${username}/${touxiang}`, window.location.href);
socketURL.protocol = socketURL.protocol.replace(location.protocol, location.protocol === 'https:' ? 'wss' : 'ws');
const socket = new WebSocket(socketURL);
部署之前先使用 caddy 来测试
npm run build-only # 编译
以下是 Caddyfile , BACKEND_URL
和 BACKEND_HOST
可自行修改,前者为后端 url, 如 http://127.0.0.1:8080 , 后者为要传递给后端的 host , 如 xxx.kexie.space
:80 {
root * ./dist
file_server
handle_path /api/* {
reverse_proxy "{$BACKEND_URL}" {
header_up host "{$BACKEND_HOST}"
}
}
}
This template should help get you started developing with Vue 3 in Vite.
VSCode + Volar (and disable Vetur).
TypeScript cannot handle type information for .vue
imports by default, so we replace the tsc
CLI with vue-tsc
for type checking. In editors, we need Volar to make the TypeScript language service aware of .vue
types.
See Vite Configuration Reference.
npm install
npm run dev
npm run build