Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,84 +1,77 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
const cheerio = require('cheerio');
|
|
|
|
|
|
|
| 4 |
const app = express();
|
| 5 |
const PORT = 7860;
|
| 6 |
|
| 7 |
-
//
|
|
|
|
| 8 |
app.use((req, res, next) => {
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
});
|
| 12 |
|
| 13 |
-
//
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
//
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
res.status(500).json({
|
| 26 |
-
|
| 27 |
-
|
| 28 |
});
|
|
|
|
| 29 |
});
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
try {
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
});
|
| 42 |
}
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 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 |
-
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
const cheerio = require('cheerio');
|
| 4 |
+
const { parseStudentData } = require('./contentModel.js');
|
| 5 |
+
|
| 6 |
const app = express();
|
| 7 |
const PORT = 7860;
|
| 8 |
|
| 9 |
+
// Middleware
|
| 10 |
+
app.use(express.static('public'));
|
| 11 |
app.use((req, res, next) => {
|
| 12 |
+
res.header({
|
| 13 |
+
"Access-Control-Allow-Origin": "*",
|
| 14 |
+
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
| 15 |
+
"Access-Control-Allow-Headers": "Content-Type"
|
| 16 |
+
});
|
| 17 |
+
next();
|
| 18 |
});
|
| 19 |
|
| 20 |
+
// API Endpoint
|
| 21 |
+
app.get('/fetch-regno-:regno', async (req, res) => {
|
| 22 |
+
try {
|
| 23 |
+
const { regno } = req.params;
|
| 24 |
+
|
| 25 |
+
// Validate registration number
|
| 26 |
+
if (!/^\d{11}$/.test(regno)) {
|
| 27 |
+
return res.status(400).json({
|
| 28 |
+
error: "Invalid registration number",
|
| 29 |
+
example: "22104134026"
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// Fetch data with retries
|
| 34 |
+
const html = await fetchWithRetries(regno);
|
| 35 |
+
if (!html) return res.status(404).json({ error: "Result not found" });
|
| 36 |
|
| 37 |
+
// Parse and return
|
| 38 |
+
const result = parseStudentData(html, regno);
|
| 39 |
+
res.json(result);
|
| 40 |
+
|
| 41 |
+
} catch (error) {
|
| 42 |
+
console.error(error);
|
| 43 |
res.status(500).json({
|
| 44 |
+
error: "Server error",
|
| 45 |
+
details: error.message
|
| 46 |
});
|
| 47 |
+
}
|
| 48 |
});
|
| 49 |
|
| 50 |
+
// Fetch helper with exponential backoff
|
| 51 |
+
async function fetchWithRetries(regno) {
|
| 52 |
+
const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
|
| 53 |
+
let retries = 3;
|
| 54 |
+
let delay = 1000;
|
| 55 |
+
|
| 56 |
+
for (let attempt = 0; attempt < retries; attempt++) {
|
| 57 |
try {
|
| 58 |
+
const response = await axios.get(url, {
|
| 59 |
+
timeout: 10000,
|
| 60 |
+
headers: {
|
| 61 |
+
'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',
|
| 62 |
+
'Referer': 'https://results.beup.ac.in/'
|
|
|
|
| 63 |
}
|
| 64 |
+
});
|
| 65 |
|
| 66 |
+
if (response.data.includes("No Record Found !!!")) return null;
|
| 67 |
+
return response.data;
|
| 68 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
} catch (error) {
|
| 70 |
+
if (attempt === retries - 1) throw error;
|
| 71 |
+
await new Promise(resolve => setTimeout(resolve, delay));
|
| 72 |
+
delay *= 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
|
| 77 |
+
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|
|
|
|
|
|
|