WalleGriffkinder commited on
Commit
11e4d16
·
verified ·
1 Parent(s): 6e5fce3

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +22 -36
index.js CHANGED
@@ -1,65 +1,51 @@
1
  import express from 'express';
2
  import fetch from 'node-fetch';
3
- import bodyParser from 'body-parser';
4
 
5
  const app = express();
6
 
7
- // Отключаем парсинг body, чтобы получать сырые данные
8
- app.use(bodyParser.raw({type: '*/*', limit: '50mb'}));
9
 
10
  const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
11
 
12
  app.use(async (req, res) => {
13
- if (req.url === '/') return res.send('OK');
14
-
15
  try {
16
- // Получаем target URL, убирая наш домен из начала
17
  const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
 
 
18
  const targetUrl = fullUrl.substring(fullUrl.indexOf(MY_DOMAIN) + MY_DOMAIN.length);
19
 
20
- // Базовые CORS заголовки
21
- res.setHeader('Access-Control-Allow-Origin', '*');
22
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
23
- res.setHeader('Access-Control-Allow-Headers', '*');
24
-
25
- // Для OPTIONS запросов сразу возвращаем ответ
26
- if (req.method === 'OPTIONS') {
27
- return res.status(200).end();
28
- }
29
-
30
- // Копируем оригинальные заголовки
31
  const headers = {...req.headers};
32
  delete headers['host'];
33
- delete headers['content-length'];
34
  delete headers['transfer-encoding'];
35
  delete headers['connection'];
36
 
37
- // Делаем запрос
38
- const fetchOptions = {
39
  method: req.method,
40
  headers,
41
- redirect: 'follow'
42
- };
43
-
44
- // Добавляем тело только для не-GET запросов
45
- if (req.method !== 'GET' && req.body) {
46
- fetchOptions.body = req.body;
47
- }
48
-
49
- const response = await fetch(targetUrl, fetchOptions);
50
 
51
- // Копируем статус
52
- res.status(response.status);
53
-
54
- // Копируем заголовки ответа
55
  for (const [key, value] of response.headers) {
56
  if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
57
  res.setHeader(key, value);
58
  }
59
  }
60
 
61
- // Передаём тело ответа
62
- response.body.pipe(res);
 
 
 
 
 
 
 
 
 
63
 
64
  } catch (error) {
65
  console.error(error);
@@ -69,4 +55,4 @@ app.use(async (req, res) => {
69
 
70
  app.listen(7860, '0.0.0.0', () => {
71
  console.log('Server running on port 7860');
72
- });
 
1
  import express from 'express';
2
  import fetch from 'node-fetch';
 
3
 
4
  const app = express();
5
 
6
+ // Принимаем сырые данные
7
+ app.use(express.raw({type: '*/*', limit: '50mb'}));
8
 
9
  const MY_DOMAIN = 'https://wallegriffkinder-testinghf23.hf.space/';
10
 
11
  app.use(async (req, res) => {
 
 
12
  try {
 
13
  const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
14
+
15
+ // Просто берем всё после нашего домена, без лишних проверок
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['transfer-encoding'];
22
  delete headers['connection'];
23
 
24
+ const response = await fetch(targetUrl, {
 
25
  method: req.method,
26
  headers,
27
+ body: req.method !== 'GET' ? req.body : undefined,
28
+ redirect: 'follow',
29
+ });
 
 
 
 
 
 
30
 
31
+ // Копируем заголовки
 
 
 
32
  for (const [key, value] of response.headers) {
33
  if (!['connection', 'transfer-encoding'].includes(key.toLowerCase())) {
34
  res.setHeader(key, value);
35
  }
36
  }
37
 
38
+ // CORS
39
+ res.setHeader('Access-Control-Allow-Origin', '*');
40
+ res.setHeader('Access-Control-Allow-Methods', '*');
41
+ res.setHeader('Access-Control-Allow-Headers', '*');
42
+
43
+ if (req.method === 'OPTIONS') {
44
+ return res.status(200).end();
45
+ }
46
+
47
+ res.status(response.status);
48
+ return response.body.pipe(res);
49
 
50
  } catch (error) {
51
  console.error(error);
 
55
 
56
  app.listen(7860, '0.0.0.0', () => {
57
  console.log('Server running on port 7860');
58
+ });