-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (35 loc) · 1.11 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const express = require('express');
const path = require('path');
const { generateReport } = require('./reportGenerator');
const app = express();
const PORT = 3000;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Welcome to the EduNexus API!');
});
app.post('/generate-report', async (req, res) => {
try {
const studentId = req.body.studentId;
const reportPath = await generateReport(studentId);
const absoluteReportPath = path.resolve(reportPath);
res.setHeader('Content-Disposition', 'attachment; filename=report.pdf');
res.sendFile(absoluteReportPath, (err) => {
if (err) {
console.error(err);
res.status(500).send('Error sending report');
} else {
fs.unlink(absoluteReportPath, (unlinkErr) => {
if (unlinkErr) {
console.error('Error deleting report:', unlinkErr);
}
});
}
});
} catch (error) {
console.error('Error generating report:', error);
res.status(500).send('Error generating report');
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});