File size: 2,116 Bytes
7385ed3
 
 
b932588
 
7385ed3
 
 
b932588
 
26bacb1
b932588
 
 
 
 
 
26bacb1
 
b932588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7763a3b
b932588
 
 
 
 
 
7763a3b
b932588
 
7763a3b
b932588
7763a3b
 
b932588
 
 
 
 
 
 
7385ed3
b932588
 
 
 
 
7763a3b
b932588
7763a3b
b932588
 
 
7385ed3
b932588
 
 
7385ed3
b932588
 
7385ed3
b932588
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const { parseStudentData } = require('./contentModel.js');

const app = express();
const PORT = 7860;

// Middleware
app.use(express.static('public'));
app.use((req, res, next) => {
  res.header({
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type"
  });
  next();
});

// API Endpoint
app.get('/fetch-regno-:regno', async (req, res) => {
  try {
    const { regno } = req.params;
    
    // Validate registration number
    if (!/^\d{11}$/.test(regno)) {
      return res.status(400).json({
        error: "Invalid registration number",
        example: "22104134026"
      });
    }

    // Fetch data with retries
    const html = await fetchWithRetries(regno);
    if (!html) return res.status(404).json({ error: "Result not found" });

    // Parse and return
    const result = parseStudentData(html, regno);
    res.json(result);

  } catch (error) {
    console.error(error);
    res.status(500).json({
      error: "Server error",
      details: error.message
    });
  }
});

// Fetch helper with exponential backoff
async function fetchWithRetries(regno) {
  const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
  let retries = 3;
  let delay = 1000;

  for (let attempt = 0; attempt < retries; attempt++) {
    try {
      const response = await axios.get(url, {
        timeout: 10000,
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
          'Referer': 'https://results.beup.ac.in/'
        }
      });

      if (response.data.includes("No Record Found !!!")) return null;
      return response.data;
      
    } catch (error) {
      if (attempt === retries - 1) throw error;
      await new Promise(resolve => setTimeout(resolve, delay));
      delay *= 2;
    }
  }
}

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));