Skip to content

Commit

Permalink
#38
Browse files Browse the repository at this point in the history
- `preset/node`: небольшие правки проксирования (patch)
  • Loading branch information
krutoo committed May 6, 2024
1 parent b921668 commit 306c8a6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
10 changes: 4 additions & 6 deletions examples/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

## Запуск

Expand Down
22 changes: 17 additions & 5 deletions examples/node/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''),
},
]);
}),
);

Expand Down
10 changes: 4 additions & 6 deletions examples/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

## Запуск

Expand Down
26 changes: 18 additions & 8 deletions src/preset/node/providers/main-express-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
}

Expand Down

0 comments on commit 306c8a6

Please sign in to comment.