Rooni commited on
Commit
40f1235
·
verified ·
1 Parent(s): 57f03d4

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +57 -1
server.js CHANGED
@@ -1,6 +1,6 @@
1
  const express = require('express');
2
  const axios = require('axios');
3
-
4
  const app = express();
5
  app.use(express.json());
6
 
@@ -32,6 +32,62 @@ app.post('/fetch-sheet', async (req, res) => {
32
  }
33
  });
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  const port = 7860;
36
  app.listen(port, () => {
37
  console.log(`API сервер запущен на порту ${port}`);
 
1
  const express = require('express');
2
  const axios = require('axios');
3
+
4
  const app = express();
5
  app.use(express.json());
6
 
 
32
  }
33
  });
34
 
35
+ app.post('/search', async (req, res) => {
36
+ const { key, list, search, search_key, max, page } = req.body;
37
+
38
+ if (!key || !list || !search || !search_key || !page || !max) {
39
+ return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
40
+ }
41
+
42
+ const encodedKey = encodeURIComponent(key);
43
+ const encodedList = encodeURIComponent(list);
44
+
45
+ try {
46
+ const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
47
+ const response = await axios.get(url);
48
+ let data = response.data;
49
+
50
+ // Фильтрация данных по строке поиска
51
+ data = data.filter(item => item[search_key] && item[search_key].toString().toLowerCase().includes(search.toLowerCase()));
52
+
53
+ // Разделение данных на страницы
54
+ const startIndex = (page - 1) * max;
55
+ const endIndex = startIndex + max;
56
+ const paginatedData = data.slice(startIndex, endIndex);
57
+
58
+ res.json(paginatedData);
59
+ } catch (error) {
60
+ console.error(error);
61
+ res.status(500).json({ error: '❌ Произошла ошибка сервера при поиске данных.' });
62
+ }
63
+ });
64
+
65
+ app.post('/random', async (req, res) => {
66
+ const { key, list, num } = req.body;
67
+
68
+ if (!key || !list || !num) {
69
+ return res.status(400).json({ error: '❌ Ошибка данных, повторите попытку.' });
70
+ }
71
+
72
+ const encodedKey = encodeURIComponent(key);
73
+ const encodedList = encodeURIComponent(list);
74
+
75
+ try {
76
+ const url = `https://opensheet.elk.sh/${encodedKey}/${encodedList}`;
77
+ const response = await axios.get(url);
78
+ let data = response.data;
79
+
80
+ // Получение случайных элементов из списка
81
+ const shuffled = data.sort(() => 0.5 - Math.random());
82
+ let selected = shuffled.slice(0, num);
83
+
84
+ res.json(selected);
85
+ } catch (error) {
86
+ console.error(error);
87
+ res.status(500).json({ error: '❌ Произошла ошибка сервера при получении случайных данных.' });
88
+ }
89
+ });
90
+
91
  const port = 7860;
92
  app.listen(port, () => {
93
  console.log(`API сервер запущен на порту ${port}`);