Spaces:
Sleeping
Sleeping
const express = require('express'); | |
const axios = require('axios'); | |
const { parseStudentData } = require('./contentModel.js'); | |
const app = express(); | |
const PORT = 7860; | |
// Middleware | |
app.use(express.static('public')); | |
app.use(express.urlencoded({ extended: true })); | |
app.use((req, res, next) => { | |
res.header({ | |
"Access-Control-Allow-Origin": "106.77.189.65", | |
"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 format", | |
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) { | |
res.status(500).json({ | |
error: "Server error", | |
details: error.message | |
}); | |
} | |
}); | |
// Search Endpoint (Web Interface) | |
app.get('/search', async (req, res) => { | |
try { | |
const regno = req.query.regno; | |
if (!regno) return res.redirect('/'); | |
const apiResponse = await axios.get(`http://localhost:${PORT}/fetch-regno-${regno}`); | |
const result = apiResponse.data; | |
res.send(` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Result for ${regno}</title> | |
<link rel="stylesheet" href="/style.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Student Result</h1> | |
<div class="result-box"> | |
<p><strong>Name:</strong> ${result.student_name}</p> | |
<p><strong>Registration No:</strong> ${regno}</p> | |
<p><strong>SGPA:</strong> ${result.sgpa}</p> | |
<p><strong>College:</strong> ${result.college_name}</p> | |
<p><strong>Course:</strong> ${result.course_name}</p> | |
</div> | |
<a href="/" class="button">Check Another</a> | |
</div> | |
</body> | |
</html> | |
`); | |
} catch (error) { | |
res.status(error.response?.status || 500).send(` | |
<!DOCTYPE html> | |
<html> | |
<head><title>Error</title><link rel="stylesheet" href="/style.css"></head> | |
<body> | |
<div class="container"> | |
<h1>Error ${error.response?.status || ''}</h1> | |
<div class="error-box"> | |
<p>${error.response?.data?.error || 'Something went wrong'}</p> | |
<a href="/" class="button">Try Again</a> | |
</div> | |
</div> | |
</body> | |
</html> | |
`); | |
} | |
}); | |
// Helper function with retries | |
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}`)); |