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'); | ||||
|   | ||||
| @@ -21,7 +21,8 @@ | ||||
|     "dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ", | ||||
|     "clean": "rm -rf dist", | ||||
|     "prepub": "envision switch root", | ||||
|     "pub": "npm run build && envision pack -p -u" | ||||
|     "pub": "npm run build && envision pack -p -u", | ||||
|     "download": "ev app download -i root/assistant-base-app" | ||||
|   }, | ||||
|   "keywords": [], | ||||
|   "author": "abearxiong <xiongxiao@xiongxiao.me>", | ||||
|   | ||||
| @@ -48,8 +48,8 @@ export const setConfig = (config?: AssistantConfig) => { | ||||
|   if (!config) { | ||||
|     return assistantConfig; | ||||
|   } | ||||
|   assistantConfig = config; | ||||
|   fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); | ||||
|   assistantConfig = { ...assistantConfig, ...config }; | ||||
|   fs.writeFileSync(configPath, JSON.stringify(assistantConfig, null, 2)); | ||||
|   return assistantConfig; | ||||
| }; | ||||
| type AppConfig = { | ||||
| @@ -69,7 +69,8 @@ export const getAppConfig = (): AppConfig => { | ||||
| }; | ||||
|  | ||||
| export const setAppConfig = (config: AppConfig) => { | ||||
|   fs.writeFileSync(appConfigPath, JSON.stringify(config, null, 2)); | ||||
|   const _config = getAppConfig(); | ||||
|   fs.writeFileSync(appConfigPath, JSON.stringify({ ..._config, ...config }, null, 2)); | ||||
|   return config; | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -17,37 +17,50 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp | ||||
|     console.log('handle by router'); | ||||
|     return; | ||||
|   } | ||||
|   // client, api, v1, serve 开头的拦截 | ||||
|   const apiProxyList = assistantConfig?.apiProxyList || []; | ||||
|   const defaultApiProxy = createApiProxy(assistantConfig?.pageApi || 'https://kevisual.xiongxiao.me'); | ||||
|   const apiBackendProxy = [...apiProxyList, ...defaultApiProxy].find((item) => pathname.startsWith(item.path)); | ||||
|   if (apiBackendProxy) { | ||||
|     console.log('apiBackendProxy', apiBackendProxy); | ||||
|     console.log('apiBackendProxy', apiBackendProxy, req.url); | ||||
|     return apiProxy(req, res, { | ||||
|       path: apiBackendProxy.path, | ||||
|       target: apiBackendProxy.target, | ||||
|     }); | ||||
|   } | ||||
|   // client, api, v1, serve 开头的拦截 | ||||
|   const urls = pathname.split('/'); | ||||
|   const [_, _user, _app] = urls; | ||||
|   if (!_app) { | ||||
|     res.statusCode = 404; | ||||
|     res.end('Not Found Proxy'); | ||||
|     return; | ||||
|   } | ||||
|   if (_app && urls.length === 3) { | ||||
|     // 重定向到 | ||||
|     res.writeHead(302, { Location: `${req.url}/` }); | ||||
|     return res.end(); | ||||
|   } | ||||
|   const proxyApiList = assistantConfig?.proxy || []; | ||||
|   const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path)); | ||||
|   if (proxyApi) { | ||||
|     console.log('proxyApi', proxyApi, pathname); | ||||
|     const { user, key } = proxyApi; | ||||
|     return fileProxy(req, res, { | ||||
|       path: proxyApi.path, | ||||
|       rootPath: appDir, | ||||
|       indexPath: `${user}/${key}/index.html`, | ||||
|       path: proxyApi.path, // 代理路径, 比如/root/center | ||||
|       rootPath: appDir, // 根路径 | ||||
|       indexPath: `${user}/${key}/index.html`, // 首页路径 | ||||
|     }); | ||||
|   } | ||||
|   const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path)); | ||||
|   if (localProxyProxy) { | ||||
|     console.log('localProxyProxy', localProxyProxy, req.url); | ||||
|     return fileProxy(req, res, { | ||||
|       path: localProxyProxy.path, | ||||
|       rootPath: process.cwd(), | ||||
|       indexPath: localProxyProxy.indexPath, | ||||
|     }); | ||||
|   } | ||||
|   console.log('handle by router 404'); | ||||
|   console.log('handle by router 404', req.url); | ||||
|   res.statusCode = 404; | ||||
|   res.end('Not Found Proxy'); | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user