From 306c8a6247ec59353d564973be377dfd7d76ad2f Mon Sep 17 00:00:00 2001 From: krutoo Date: Mon, 6 May 2024 12:30:23 +0500 Subject: [PATCH] #38 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `preset/node`: небольшие правки проксирования (patch) --- examples/node/README.md | 10 +++---- examples/node/src/app/index.ts | 22 ++++++++++++---- examples/web/README.md | 10 +++---- src/preset/node/providers/main-express-app.ts | 26 +++++++++++++------ 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/examples/node/README.md b/examples/node/README.md index e7411dc..164a111 100644 --- a/examples/node/README.md +++ b/examples/node/README.md @@ -4,12 +4,10 @@ ## Предварительная настройка -1. Перейти в директорию проекта `@sima-land/isomorph` -2. Выполнить `npm install` -3. Выполнить `npm run build` -4. Выполнить `npm pack` -5. Перейти в директорию данного приложения -6. Выполнить `npm install` +```bash +npm install # установка зависимостей +npm run preparing # установка локальной версии @sima-land/isomorph +``` ## Запуск diff --git a/examples/node/src/app/index.ts b/examples/node/src/app/index.ts index 69246fe..d26da3e 100644 --- a/examples/node/src/app/index.ts +++ b/examples/node/src/app/index.ts @@ -16,11 +16,23 @@ export function MainApp() { override(TOKEN.Lib.Express.pageRoutes, providePageRoutes); // добавляем проксирование - override(TOKEN.Lib.Http.Serve.Proxy.config, () => ({ - filter: '/api', - target: 'https://jsonplaceholder.typicode.com/', - pathRewrite: pathname => pathname.replace('/api', ''), - })); + override(TOKEN.Lib.Http.Serve.Proxy.config, () => [ + { + filter: '/api/v3', + target: 'https://www.sima-land.ru/api/v3/', + pathRewrite: pathname => pathname.replace('/api/v3', ''), + }, + { + filter: '/api/v6', + target: 'https://www.sima-land.ru/api/v6/', + pathRewrite: pathname => pathname.replace('/api/v6', ''), + }, + { + filter: '/api', + target: 'https://jsonplaceholder.typicode.com/', + pathRewrite: pathname => pathname.replace('/api', ''), + }, + ]); }), ); diff --git a/examples/web/README.md b/examples/web/README.md index 32616a4..9cf18a7 100644 --- a/examples/web/README.md +++ b/examples/web/README.md @@ -4,12 +4,10 @@ ## Предварительная настройка -1. Перейти в директорию проекта `@sima-land/isomorph` -2. Выполнить `npm install` -3. Выполнить `npm run build` -4. Выполнить `npm pack` -5. Перейти в директорию данного приложения -6. Выполнить `npm install` +```bash +npm install # установка зависимостей +npm run preparing # установка локальной версии @sima-land/isomorph +``` ## Запуск diff --git a/src/preset/node/providers/main-express-app.ts b/src/preset/node/providers/main-express-app.ts index 886605d..3950560 100644 --- a/src/preset/node/providers/main-express-app.ts +++ b/src/preset/node/providers/main-express-app.ts @@ -29,14 +29,24 @@ export function provideMainExpressApp(resolve: Resolve): express.Application { const proxyPaths = Array.isArray(filter) ? filter : [filter]; - app.use( - createProxyMiddleware({ - target, - changeOrigin: true, - pathRewrite, - pathFilter: inputPath => proxyPaths.some(proxyPath => inputPath.startsWith(proxyPath)), - }), - ); + const proxyMiddleware = createProxyMiddleware({ + target, + changeOrigin: true, + pathRewrite, + + // ВАЖНО: не используем pathFilter тк если в проекте есть webpack-dev-server, + // он тащит за собой http-proxy-middleware@2 который не поддерживает pathFilter + // pathFilter: inputPath => proxyPaths.some(proxyPath => inputPath.startsWith(proxyPath)), + }); + + app.use((req, res, next) => { + // ВАЖНО: поскольку не используем pathFilter - фильтруем запросы проверкой + if (proxyPaths.some(proxyPath => req.path.startsWith(proxyPath))) { + proxyMiddleware(req, res, next); + } else { + next(); + } + }); } }