Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -4,12 +4,19 @@ const cheerio = require('cheerio');
|
|
| 4 |
const app = express();
|
| 5 |
const PORT = 7860;
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
app.get('/fetch-regno-:regno', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
| 8 |
try {
|
| 9 |
-
const { regno } = req.params;
|
| 10 |
const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
|
| 11 |
|
| 12 |
-
// Fetch the page with headers to mimic browser
|
| 13 |
const response = await axios.get(url, {
|
| 14 |
headers: {
|
| 15 |
'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',
|
|
@@ -19,22 +26,32 @@ app.get('/fetch-regno-:regno', async (req, res) => {
|
|
| 19 |
|
| 20 |
const $ = cheerio.load(response.data);
|
| 21 |
|
| 22 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
const result = {
|
| 24 |
-
name:
|
| 25 |
-
regno:
|
| 26 |
-
sgpa:
|
| 27 |
};
|
| 28 |
|
| 29 |
-
console.log('Scraped Result:', result);
|
| 30 |
res.json(result);
|
| 31 |
-
|
| 32 |
} catch (error) {
|
| 33 |
-
console.error(
|
| 34 |
-
res.status(500).json({
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
});
|
| 37 |
|
| 38 |
app.listen(PORT, () => {
|
|
|
|
| 39 |
console.log(`Server running on port ${PORT}`);
|
| 40 |
});
|
|
|
|
| 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',
|
|
|
|
| 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 |
});
|