boyuehasfj-vue3-html/vite.config.ts

134 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

2025-06-02 21:36:36 +08:00
import { fileURLToPath, URL } from 'node:url'
import fs from 'node:fs'
import path from 'node:path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
// 自定义中间件,处理路由问题
{
name: 'handle-spa-fallback',
configureServer(server) {
// 返回中间件处理函数
return () => {
server.middlewares.use((req, res, next) => {
// 如果是前端路由路径直接返回index.html
2025-06-02 21:36:36 +08:00
if (
req.url === '/hasfjlaw' ||
req.url === '/hasfjcase' ||
req.url === '/hasfjform' ||
req.url === '/qrcodes'
) {
const indexHtml = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8')
2025-06-02 21:36:36 +08:00
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.end(indexHtml)
return
}
next()
})
}
2025-06-02 21:36:36 +08:00
},
},
2025-06-02 21:36:36 +08:00
],
resolve: {
alias: {
2025-06-02 21:36:36 +08:00
'@': fileURLToPath(new URL('./src', import.meta.url)),
2025-06-02 21:36:36 +08:00
},
},
server: {
port: 80, // 指定端口为80
host: '0.0.0.0', // 允许外部访问
proxy: {
// 配置跨域
'/api': {
2025-06-02 21:36:36 +08:00
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端服务地址
2025-06-02 21:36:36 +08:00
changeOrigin: true, // 支持跨域
rewrite: (path) => path.replace(/^\/api/, ''), // 移除/api前缀
configure: (proxy, options) => {
// 添加代理错误处理
proxy.on('error', (err, req, res) => {
2025-06-02 21:36:36 +08:00
console.error('API代理错误:', err)
2025-06-02 21:36:36 +08:00
// 返回友好的错误响应
if (!res.headersSent) {
res.writeHead(500, {
2025-06-02 21:36:36 +08:00
'Content-Type': 'application/json',
})
2025-06-02 21:36:36 +08:00
2025-06-02 21:36:36 +08:00
res.end(
JSON.stringify({
code: 500,
msg: '后端服务暂时不可用,请稍后重试',
data: null,
}),
)
2025-06-02 21:36:36 +08:00
}
2025-06-02 21:36:36 +08:00
})
},
2025-06-02 21:36:36 +08:00
},
// 添加司法局后台管理接口代理
'/hasfj': {
2025-06-02 21:36:36 +08:00
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端服务地址
2025-06-02 21:36:36 +08:00
changeOrigin: true,
rewrite: (path) => path, // 保持路径不变
configure: (proxy, options) => {
// 添加代理错误处理
proxy.on('error', (err, req, res) => {
2025-06-02 21:36:36 +08:00
console.error('后台管理接口代理错误:', err)
2025-06-02 21:36:36 +08:00
// 返回友好的错误响应
if (!res.headersSent) {
res.writeHead(500, {
2025-06-02 21:36:36 +08:00
'Content-Type': 'application/json',
})
2025-06-02 21:36:36 +08:00
2025-06-02 21:36:36 +08:00
res.end(
JSON.stringify({
code: 500,
msg: '后端服务暂时不可用,请稍后重试',
data: null,
}),
)
2025-06-02 21:36:36 +08:00
}
2025-06-02 21:36:36 +08:00
})
},
2025-06-02 21:36:36 +08:00
},
// 添加文件下载代理,专门处理静态资源文件请求
'/profile': {
2025-06-02 21:36:36 +08:00
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端静态资源服务地址
2025-06-02 21:36:36 +08:00
changeOrigin: true,
rewrite: (path) => path, // 保持路径不变
configure: (proxy, options) => {
proxy.on('error', (err, req, res) => {
2025-06-02 21:36:36 +08:00
console.error('文件下载代理错误:', err)
2025-06-02 21:36:36 +08:00
if (!res.headersSent) {
res.writeHead(500, {
2025-06-02 21:36:36 +08:00
'Content-Type': 'application/json',
})
2025-06-02 21:36:36 +08:00
2025-06-02 21:36:36 +08:00
res.end(
JSON.stringify({
code: 500,
msg: '文件下载失败,请稍后重试',
data: null,
}),
)
2025-06-02 21:36:36 +08:00
}
2025-06-02 21:36:36 +08:00
})
},
},
},
},
2025-06-02 21:36:36 +08:00
})