boyuehasfj-vue3-html/vite.config.ts
2025-07-05 14:40:45 +08:00

134 lines
4.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
if (
req.url === '/hasfjlaw' ||
req.url === '/hasfjcase' ||
req.url === '/hasfjform' ||
req.url === '/qrcodes'
) {
const indexHtml = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8')
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.end(indexHtml)
return
}
next()
})
}
},
},
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
server: {
port: 80, // 指定端口为80
host: '0.0.0.0', // 允许外部访问
proxy: {
// 配置跨域
'/api': {
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端服务地址
changeOrigin: true, // 支持跨域
rewrite: (path) => path.replace(/^\/api/, ''), // 移除/api前缀
configure: (proxy, options) => {
// 添加代理错误处理
proxy.on('error', (err, req, res) => {
console.error('API代理错误:', err)
// 返回友好的错误响应
if (!res.headersSent) {
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
code: 500,
msg: '后端服务暂时不可用,请稍后重试',
data: null,
}),
)
}
})
},
},
// 添加司法局后台管理接口代理
'/hasfj': {
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端服务地址
changeOrigin: true,
rewrite: (path) => path, // 保持路径不变
configure: (proxy, options) => {
// 添加代理错误处理
proxy.on('error', (err, req, res) => {
console.error('后台管理接口代理错误:', err)
// 返回友好的错误响应
if (!res.headersSent) {
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
code: 500,
msg: '后端服务暂时不可用,请稍后重试',
data: null,
}),
)
}
})
},
},
// 添加文件下载代理,专门处理静态资源文件请求
'/profile': {
// target: 'http://127.0.0.1:19696', // 后端服务地址
target: 'http://222.184.49.22:19696', // 后端静态资源服务地址
changeOrigin: true,
rewrite: (path) => path, // 保持路径不变
configure: (proxy, options) => {
proxy.on('error', (err, req, res) => {
console.error('文件下载代理错误:', err)
if (!res.headersSent) {
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
code: 500,
msg: '文件下载失败,请稍后重试',
data: null,
}),
)
}
})
},
},
},
},
})