understanding commited on
Commit
7763a3b
·
verified ·
1 Parent(s): 26bacb1

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +53 -26
server.js CHANGED
@@ -4,54 +4,81 @@ const cheerio = require('cheerio');
4
  const app = express();
5
  const PORT = 7860;
6
 
7
- // Middleware to log requests
8
  app.use((req, res, next) => {
9
- console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
10
  next();
11
  });
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  app.get('/fetch-regno-:regno', async (req, res) => {
14
  const { regno } = req.params;
15
- console.log(`Fetching result for RegNo: ${regno}`);
16
 
17
  try {
18
- const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
19
-
20
- const response = await axios.get(url, {
21
- headers: {
22
- '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',
23
- 'Referer': 'https://results.beup.ac.in/'
 
 
 
 
 
 
 
 
 
 
24
  }
25
- });
26
 
27
  const $ = cheerio.load(response.data);
28
 
29
- // Clean up the data
30
- const cleanText = (selector) => {
31
- return $(selector).text()
32
- .replace(/\n/g, ' ')
33
- .replace(/\s+/g, ' ')
34
- .trim();
35
- };
36
-
37
  const result = {
38
- name: cleanText('#lblName'),
39
- regno: regno,
40
- sgpa: cleanText('#lblSGPA').replace(/Cur\. CGPA.*/, '') // Remove unwanted text
 
41
  };
42
 
 
 
 
43
  res.json(result);
44
 
45
  } catch (error) {
46
- console.error(`Error fetching ${regno}:`, error.message);
47
- res.status(500).json({
 
48
  error: 'Failed to fetch result',
49
- regno: regno
 
50
  });
51
  }
52
  });
53
 
54
  app.listen(PORT, () => {
55
- console.log(`==== Application Startup at ${new Date().toISOString()} ====`);
56
- console.log(`Server running on port ${PORT}`);
57
  });
 
4
  const app = express();
5
  const PORT = 7860;
6
 
7
+ // Enhanced logging middleware
8
  app.use((req, res, next) => {
9
+ console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl}`);
10
  next();
11
  });
12
 
13
+ // Data cleaning utility
14
+ const cleanData = (text) => {
15
+ return text
16
+ .replace(/\n/g, ' ') // Remove newlines
17
+ .replace(/\s+/g, ' ') // Collapse whitespace
18
+ .replace(/\\/g, '') // Remove backslashes
19
+ .trim();
20
+ };
21
+
22
+ // Improved error handling middleware
23
+ app.use((err, req, res, next) => {
24
+ console.error(`[ERROR] ${req.method} ${req.originalUrl}: ${err.message}`);
25
+ res.status(500).json({
26
+ error: 'Internal server error',
27
+ regno: req.params.regno
28
+ });
29
+ });
30
+
31
  app.get('/fetch-regno-:regno', async (req, res) => {
32
  const { regno } = req.params;
33
+ const startTime = Date.now();
34
 
35
  try {
36
+ // Validate registration number format
37
+ if (!/^\d{11}$/.test(regno)) {
38
+ return res.status(400).json({
39
+ error: 'Invalid registration number format',
40
+ regno
41
+ });
42
+ }
43
+
44
+ const response = await axios.get(
45
+ `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`,
46
+ {
47
+ headers: {
48
+ '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',
49
+ 'Referer': 'https://results.beup.ac.in/'
50
+ },
51
+ timeout: 10000
52
  }
53
+ );
54
 
55
  const $ = cheerio.load(response.data);
56
 
57
+ // Extract and clean data
 
 
 
 
 
 
 
58
  const result = {
59
+ name: cleanData($('#lblName').text()),
60
+ regno: regno, // Using parameter instead of page value
61
+ sgpa: cleanData($('#lblSGPA').text())
62
+ .match(/\d+\.\d+/)?.[0] || 'N/A' // Extract numeric SGPA
63
  };
64
 
65
+ // Log performance
66
+ console.log(`[SUCCESS] ${regno} (${Date.now() - startTime}ms)`);
67
+
68
  res.json(result);
69
 
70
  } catch (error) {
71
+ console.error(`[FAILED] ${regno}: ${error.message}`);
72
+ const status = error.response?.status || 500;
73
+ res.status(status).json({
74
  error: 'Failed to fetch result',
75
+ regno,
76
+ details: status === 404 ? 'Result not found' : 'Server error'
77
  });
78
  }
79
  });
80
 
81
  app.listen(PORT, () => {
82
+ console.log(`==== Server Started at ${new Date().toISOString()} ====`);
83
+ console.log(`Listening on port ${PORT}`);
84
  });