Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 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();
|
|
@@ -8,11 +7,11 @@ 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 |
});
|
|
@@ -25,7 +24,7 @@ app.get('/fetch-regno-:regno', async (req, res) => {
|
|
| 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 |
}
|
|
@@ -39,7 +38,6 @@ app.get('/fetch-regno-:regno', async (req, res) => {
|
|
| 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
|
|
@@ -47,7 +45,58 @@ app.get('/fetch-regno-:regno', async (req, res) => {
|
|
| 47 |
}
|
| 48 |
});
|
| 49 |
|
| 50 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
async function fetchWithRetries(regno) {
|
| 52 |
const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
|
| 53 |
let retries = 3;
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const axios = require('axios');
|
|
|
|
| 3 |
const { parseStudentData } = require('./contentModel.js');
|
| 4 |
|
| 5 |
const app = express();
|
|
|
|
| 7 |
|
| 8 |
// Middleware
|
| 9 |
app.use(express.static('public'));
|
| 10 |
+
app.use(express.urlencoded({ extended: true }));
|
| 11 |
app.use((req, res, next) => {
|
| 12 |
res.header({
|
| 13 |
"Access-Control-Allow-Origin": "*",
|
| 14 |
+
"Access-Control-Allow-Methods": "GET, OPTIONS"
|
|
|
|
| 15 |
});
|
| 16 |
next();
|
| 17 |
});
|
|
|
|
| 24 |
// Validate registration number
|
| 25 |
if (!/^\d{11}$/.test(regno)) {
|
| 26 |
return res.status(400).json({
|
| 27 |
+
error: "Invalid registration number format",
|
| 28 |
example: "22104134026"
|
| 29 |
});
|
| 30 |
}
|
|
|
|
| 38 |
res.json(result);
|
| 39 |
|
| 40 |
} catch (error) {
|
|
|
|
| 41 |
res.status(500).json({
|
| 42 |
error: "Server error",
|
| 43 |
details: error.message
|
|
|
|
| 45 |
}
|
| 46 |
});
|
| 47 |
|
| 48 |
+
// Search Endpoint (Web Interface)
|
| 49 |
+
app.get('/search', async (req, res) => {
|
| 50 |
+
try {
|
| 51 |
+
const regno = req.query.regno;
|
| 52 |
+
if (!regno) return res.redirect('/');
|
| 53 |
+
|
| 54 |
+
const apiResponse = await axios.get(`http://localhost:${PORT}/fetch-regno-${regno}`);
|
| 55 |
+
const result = apiResponse.data;
|
| 56 |
+
|
| 57 |
+
res.send(`
|
| 58 |
+
<!DOCTYPE html>
|
| 59 |
+
<html>
|
| 60 |
+
<head>
|
| 61 |
+
<title>Result for ${regno}</title>
|
| 62 |
+
<link rel="stylesheet" href="/style.css">
|
| 63 |
+
</head>
|
| 64 |
+
<body>
|
| 65 |
+
<div class="container">
|
| 66 |
+
<h1>Student Result</h1>
|
| 67 |
+
<div class="result-box">
|
| 68 |
+
<p><strong>Name:</strong> ${result.student_name}</p>
|
| 69 |
+
<p><strong>Registration No:</strong> ${regno}</p>
|
| 70 |
+
<p><strong>SGPA:</strong> ${result.sgpa}</p>
|
| 71 |
+
<p><strong>College:</strong> ${result.college_name}</p>
|
| 72 |
+
<p><strong>Course:</strong> ${result.course_name}</p>
|
| 73 |
+
</div>
|
| 74 |
+
<a href="/" class="button">Check Another</a>
|
| 75 |
+
</div>
|
| 76 |
+
</body>
|
| 77 |
+
</html>
|
| 78 |
+
`);
|
| 79 |
+
|
| 80 |
+
} catch (error) {
|
| 81 |
+
res.status(error.response?.status || 500).send(`
|
| 82 |
+
<!DOCTYPE html>
|
| 83 |
+
<html>
|
| 84 |
+
<head><title>Error</title><link rel="stylesheet" href="/style.css"></head>
|
| 85 |
+
<body>
|
| 86 |
+
<div class="container">
|
| 87 |
+
<h1>Error ${error.response?.status || ''}</h1>
|
| 88 |
+
<div class="error-box">
|
| 89 |
+
<p>${error.response?.data?.error || 'Something went wrong'}</p>
|
| 90 |
+
<a href="/" class="button">Try Again</a>
|
| 91 |
+
</div>
|
| 92 |
+
</div>
|
| 93 |
+
</body>
|
| 94 |
+
</html>
|
| 95 |
+
`);
|
| 96 |
+
}
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
// Helper function with retries
|
| 100 |
async function fetchWithRetries(regno) {
|
| 101 |
const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
|
| 102 |
let retries = 3;
|