Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
@@ -4,54 +4,81 @@ const cheerio = require('cheerio');
|
|
4 |
const app = express();
|
5 |
const PORT = 7860;
|
6 |
|
7 |
-
//
|
8 |
app.use((req, res, next) => {
|
9 |
-
console.log(`[${new Date().toISOString()}] ${req.method} ${req.
|
10 |
next();
|
11 |
});
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
app.get('/fetch-regno-:regno', async (req, res) => {
|
14 |
const { regno } = req.params;
|
15 |
-
|
16 |
|
17 |
try {
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
-
|
26 |
|
27 |
const $ = cheerio.load(response.data);
|
28 |
|
29 |
-
//
|
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:
|
39 |
-
regno: regno,
|
40 |
-
sgpa:
|
|
|
41 |
};
|
42 |
|
|
|
|
|
|
|
43 |
res.json(result);
|
44 |
|
45 |
} catch (error) {
|
46 |
-
console.error(`
|
47 |
-
|
|
|
48 |
error: 'Failed to fetch result',
|
49 |
-
regno
|
|
|
50 |
});
|
51 |
}
|
52 |
});
|
53 |
|
54 |
app.listen(PORT, () => {
|
55 |
-
console.log(`====
|
56 |
-
console.log(`
|
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 |
});
|