Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -2,28 +2,25 @@ import express from 'express';
|
|
| 2 |
import fetch from 'node-fetch';
|
| 3 |
|
| 4 |
const app = express();
|
|
|
|
|
|
|
| 5 |
app.use(express.raw({type: '*/*'}));
|
| 6 |
|
| 7 |
const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
|
| 8 |
|
| 9 |
app.all('*', async (req, res) => {
|
| 10 |
-
if (req.url === '/')
|
| 11 |
-
return res.send('OK');
|
| 12 |
-
}
|
| 13 |
|
| 14 |
try {
|
| 15 |
-
// Получаем полный URL и убираем наш домен
|
| 16 |
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
|
| 17 |
const targetUrl = fullUrl.substring(fullUrl.indexOf(MY_DOMAIN) + MY_DOMAIN.length);
|
| 18 |
|
| 19 |
-
// Копируем все заголовки
|
| 20 |
const headers = {...req.headers};
|
| 21 |
delete headers['host'];
|
| 22 |
delete headers['content-length'];
|
| 23 |
delete headers['connection'];
|
| 24 |
delete headers['transfer-encoding'];
|
| 25 |
|
| 26 |
-
// Делаем запрос с сохранением всех параметров
|
| 27 |
const response = await fetch(targetUrl, {
|
| 28 |
method: req.method,
|
| 29 |
headers,
|
|
@@ -31,23 +28,14 @@ app.all('*', async (req, res) => {
|
|
| 31 |
redirect: 'follow',
|
| 32 |
});
|
| 33 |
|
| 34 |
-
// Копируем все заголовки ответа
|
| 35 |
response.headers.forEach((value, key) => {
|
| 36 |
if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
|
| 37 |
res.setHeader(key, value);
|
| 38 |
}
|
| 39 |
});
|
| 40 |
|
| 41 |
-
// Для потоковых ответов
|
| 42 |
-
if (response.headers.get('content-type')?.includes('text/event-stream')) {
|
| 43 |
-
res.setHeader('Content-Type', 'text/event-stream');
|
| 44 |
-
return response.body.pipe(res);
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
// Для обычных ответов
|
| 48 |
-
const data = await response.buffer();
|
| 49 |
res.status(response.status);
|
| 50 |
-
|
| 51 |
|
| 52 |
} catch (error) {
|
| 53 |
res.status(500).send('Proxy Error');
|
|
|
|
| 2 |
import fetch from 'node-fetch';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
+
|
| 6 |
+
// Принимаем всё как есть в сыром виде
|
| 7 |
app.use(express.raw({type: '*/*'}));
|
| 8 |
|
| 9 |
const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
|
| 10 |
|
| 11 |
app.all('*', async (req, res) => {
|
| 12 |
+
if (req.url === '/') return res.send('OK');
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try {
|
|
|
|
| 15 |
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
|
| 16 |
const targetUrl = fullUrl.substring(fullUrl.indexOf(MY_DOMAIN) + MY_DOMAIN.length);
|
| 17 |
|
|
|
|
| 18 |
const headers = {...req.headers};
|
| 19 |
delete headers['host'];
|
| 20 |
delete headers['content-length'];
|
| 21 |
delete headers['connection'];
|
| 22 |
delete headers['transfer-encoding'];
|
| 23 |
|
|
|
|
| 24 |
const response = await fetch(targetUrl, {
|
| 25 |
method: req.method,
|
| 26 |
headers,
|
|
|
|
| 28 |
redirect: 'follow',
|
| 29 |
});
|
| 30 |
|
|
|
|
| 31 |
response.headers.forEach((value, key) => {
|
| 32 |
if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
|
| 33 |
res.setHeader(key, value);
|
| 34 |
}
|
| 35 |
});
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
res.status(response.status);
|
| 38 |
+
response.body.pipe(res);
|
| 39 |
|
| 40 |
} catch (error) {
|
| 41 |
res.status(500).send('Proxy Error');
|