generated from tailored/router-template
	fix: fix proxy and api error
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "@kevisual/assistant-module", | ||||
|   "version": "0.0.4-beta.2", | ||||
|   "version": "0.0.4-beta.3", | ||||
|   "description": "assistant module", | ||||
|   "main": "dist/assistant-module.mjs", | ||||
|   "types": "dist/assistant-module.d.ts", | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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'); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user