MISASI commited on
Commit
00cafc8
Β·
verified Β·
1 Parent(s): 00e346e

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +18 -19
server.js CHANGED
@@ -18,7 +18,7 @@ function delayFileTermination(delay) {
18
  if (fs.existsSync('delay.txt')) {
19
  delay = parseInt(fs.readFileSync('delay.txt', 'utf8'))
20
  }else{
21
- fs.writeFile('delay.txt', 'delay:{$delay}', function(err){
22
  if(err = null){
23
  console.log('success')
24
  }else{
@@ -33,37 +33,36 @@ function delayFileTermination(delay) {
33
  console.log('File system loaded')
34
 
35
  app.post('/translate', async (req, res) => {
 
 
36
 
37
- console.log('Translate start')
38
- currentTranslations++
39
-
40
- const { text, source_lang, target_lang } = req.body
41
 
42
  try {
43
- await new Promise(resolve => setTimeout(resolve, delay))
44
 
45
- const result = await translate(text, source_lang, target_lang)
46
  const responseData = {
47
- alternatives: result.alternatives,
48
  code: 200,
49
- data: result.text,
50
- id: Math.floor(Math.random() * 10000000000),
51
  method: 'Free',
52
  source_lang,
53
  target_lang,
54
- }
55
- res.json(responseData)
56
- console.log(`"${text}" -> "${result.text}"`)
57
 
58
  } catch (error) {
59
- console.log('Translation failed : ${error}')
60
- delay = delay + 100
61
- delayFileTermination(delay)
62
- console("ν˜„μž¬ Delay νƒ€μž„:" + delay + "ms")
63
  } finally {
64
- currentTranslations-- // λ²ˆμ—­ μ²˜λ¦¬κ°€ λλ‚˜λ©΄ 1 κ°μ†Œ
65
  }
66
- })
67
 
68
  app.get('/', (req, res) => {
69
  const delayMessage = fs.existsSync('delay.txt') ? fs.readFileSync('delay.txt', 'utf8') : '0'
 
18
  if (fs.existsSync('delay.txt')) {
19
  delay = parseInt(fs.readFileSync('delay.txt', 'utf8'))
20
  }else{
21
+ fs.writeFile('delay.txt', `delay:{$delay}`, function(err){
22
  if(err = null){
23
  console.log('success')
24
  }else{
 
33
  console.log('File system loaded')
34
 
35
  app.post('/translate', async (req, res) => {
36
+ console.log('Translate start');
37
+ currentTranslations++;
38
 
39
+ const { text, source_lang, target_lang } = req.body;
 
 
 
40
 
41
  try {
42
+ await new Promise(resolve => setTimeout(resolve, delay));
43
 
44
+ const result = await translate(text, source_lang, target_lang);
45
  const responseData = {
46
+ alternatives: result.alternatives,
47
  code: 200,
48
+ data: result.text,
49
+ id: Math.floor(Math.random() * 10000000000),
50
  method: 'Free',
51
  source_lang,
52
  target_lang,
53
+ };
54
+ res.json(responseData);
55
+ console.log(`"${text}" -> "${result.text}"`);
56
 
57
  } catch (error) {
58
+ console.error(`Translation failed: ${error}`);
59
+ delay += 100; // delay 증가
60
+ delayFileTermination(delay);
61
+ console.log(`ν˜„μž¬ Delay νƒ€μž„: ${delay}ms`);
62
  } finally {
63
+ currentTranslations--; // λ²ˆμ—­ μ²˜λ¦¬κ°€ λλ‚˜λ©΄ κ°μ†Œ
64
  }
65
+ });
66
 
67
  app.get('/', (req, res) => {
68
  const delayMessage = fs.existsSync('delay.txt') ? fs.readFileSync('delay.txt', 'utf8') : '0'