custom-scripts/mccs-it/thermal/app.js

102 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 23:26:30 +00:00
const express = require('express');
2024-09-09 15:38:53 +00:00
const Thermal = require('./modules/Thermal.js');
2024-09-06 23:26:30 +00:00
const app = express();
2024-09-13 14:59:17 +00:00
const port = 3000;
2024-09-06 23:26:30 +00:00
// Middleware to serve static files (HTML)
app.use(express.static('public'));
2024-09-09 20:27:51 +00:00
// Middleware to parse JSON request bodies
app.use(express.json());
2024-09-10 19:42:42 +00:00
// Print credentials
app.post('/print-credentials', async (req, res) => {
2024-09-12 17:39:49 +00:00
try {
const { username, studentId } = req.body;
if (!username || !studentId) {
return res.status(400).json({ message: 'Username and Student ID are required.' });
}
// Generate the information string
2024-09-13 13:23:25 +00:00
const infoArray = Thermal.generateInfo.credentials(username, studentId);
2024-09-12 17:39:49 +00:00
// Generate the PDF, saved to disk
2024-09-13 14:05:07 +00:00
Thermal.generatePdf(infoArray).then((result) => {
2024-09-13 14:06:12 +00:00
res.send('PDF file written successfully.');
2024-09-13 14:05:07 +00:00
}).catch(e => {
2024-09-12 17:39:49 +00:00
console.error(e);
2024-09-12 17:42:43 +00:00
res.status(500).json({ message: 'Error writing PDF file', error: e.message });
2024-09-12 17:39:49 +00:00
});
2024-09-12 17:42:43 +00:00
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });
2024-09-12 17:39:49 +00:00
}
2024-09-06 23:26:30 +00:00
});
2024-09-12 16:24:02 +00:00
// Print temp password
app.post('/print-temp-pw', async (req, res) => {
2024-09-12 17:39:49 +00:00
try {
// Grab the password from the request body
const { password } = req.body;
// Check that the password is actually there
if (!password) {
return res.status(400).json({ message: 'Password is required.' });
}
// Generate the information string
2024-09-13 13:38:22 +00:00
const infoArray = Thermal.generateInfo.tempPassword(password);
2024-09-12 17:39:49 +00:00
// Generate the PDF, saved to disk
2024-09-13 14:05:07 +00:00
Thermal.generatePdf(infoArray).then((result) => {
2024-09-13 14:06:12 +00:00
res.send('PDF file written successfully.');
2024-09-13 14:05:07 +00:00
}).catch(e => {
2024-09-12 17:39:49 +00:00
console.error(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) {
console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message });
}
2024-09-12 16:24:02 +00:00
});
2024-09-10 19:42:42 +00:00
// Print Student MaineCC Info
app.post('/print-mcc', async (req, res) => {
2024-09-12 17:39:49 +00:00
try {
// Generate the information string
2024-09-13 13:38:22 +00:00
const infoArray = Thermal.generateInfo.maineCC();
2024-09-12 17:39:49 +00:00
// Generate the PDF, saved to disk
2024-09-13 14:05:07 +00:00
Thermal.generatePdf(infoArray).then((result) => {
2024-09-13 14:06:12 +00:00
res.send('PDF file written successfully.');
2024-09-13 14:05:07 +00:00
}).catch(e => {
2024-09-12 17:39:49 +00:00
console.error(e);
2024-09-12 17:47:59 +00:00
res.status(500).json({ message: 'Error writing PDF file', error: e.message });
2024-09-12 17:39:49 +00:00
});
2024-09-12 17:47:59 +00:00
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });
2024-09-12 17:39:49 +00:00
}
2024-09-10 19:42:42 +00:00
});
2024-09-12 19:40:41 +00:00
// Print Student MaineCC Info
app.post('/print', async (req, res) => {
try {
// Print the document
Thermal.print().then(result => {
res.send('PDF sent to printer successfully.');
}).catch(e => {
console.error('Failed to start printing process:', err);
res.status(500).json({ message: 'Failed to start printing process', error: err.message });
});
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });
}
});
2024-09-06 23:26:30 +00:00
// Start the server
app.listen(port, () => {
2024-09-12 17:39:49 +00:00
console.log(`Server running at http://localhost:${port}`);
2024-09-06 23:26:30 +00:00
});