fix: fix proxy and api error

This commit is contained in:
2025-03-10 14:30:40 +08:00
parent 2e1cf531cf
commit 01503954ad
7 changed files with 45 additions and 22 deletions

View File

@@ -1,5 +1,11 @@
import fs from 'fs';
/**
* 检查文件是否存在
* @param filePath 文件路径
* @param checkIsFile 是否检查文件类型 default: false
* @returns
*/
export const checkFileExists = (filePath: string, checkIsFile = false) => {
try {
fs.accessSync(filePath);

View File

@@ -18,7 +18,7 @@ export const defaultApiProxy = [
* @param paths ['/api/router', '/v1' ]
* @returns
*/
export const createApiProxy = (api: string, paths: string[] = ['/api/router', '/v1', '/resources']) => {
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1', '/resources']) => {
const pathList = paths.map((item) => {
return {
path: item,

View File

@@ -1,28 +1,30 @@
import http from 'http';
import send from 'send';
import fs from 'fs';
import { fileIsExist } from '@kevisual/use-config';
import path from 'path';
import { ProxyInfo } from './proxy.ts';
import { checkFileExists } from '@/file/index.ts';
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
// url开头的文件
const url = new URL(req.url, 'http://localhost');
let pathname = url.pathname.slice(1);
const [user, key, _info] = url.pathname.split('/');
const pathname = url.pathname.slice(1);
const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi;
try {
if (pathname.endsWith('/')) {
pathname = pathname + 'index.html';
}
// 检测文件是否存在如果文件不存在则返回404
let filePath = path.join(rootPath, target, pathname);
let exist = fileIsExist(filePath);
let filePath = '';
let exist = false;
if (_info) {
filePath = path.join(rootPath, target, pathname);
exist = checkFileExists(filePath, true);
}
if (!exist) {
filePath = path.join(rootPath, target, '/' + indexPath);
exist = fileIsExist(filePath);
filePath = path.join(rootPath, target, indexPath);
exist = checkFileExists(filePath, true);
}
console.log('filePath', filePath, exist);
if (!exist) {
res.statusCode = 404;
res.end('Not Found File');