add exceljs for xlsx file gen
This commit is contained in:
parent
73afeee79b
commit
68a19cafd7
139
main.js
139
main.js
|
@ -6,7 +6,8 @@ const config = require("./config/db.config.js");
|
|||
const session = require('express-session');
|
||||
const bcrypt = require('bcrypt');
|
||||
const signupRouter = require('./signup'); // Adjust path as necessary
|
||||
|
||||
const ExcelJS = require('exceljs');
|
||||
const cors = require('cors');
|
||||
|
||||
const PORT = process.env.PORT || 8080; // Default port is 5000
|
||||
|
||||
|
@ -33,11 +34,13 @@ const pool = mysql.createPool({
|
|||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors()); // Enable CORS for all origins
|
||||
// app.use(cors({
|
||||
// origin: '[ http://192.168.1.6:8081 , http://localhost:8081 ]'
|
||||
// }));
|
||||
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
|
@ -52,7 +55,20 @@ app.use(express.static(path.join(__dirname, 'static')));
|
|||
|
||||
// Use the signup router
|
||||
app.use(signupRouter);
|
||||
|
||||
|
||||
|
||||
app.get('/x', function (request, response) {
|
||||
if (request.session.loggedin) {
|
||||
// Output username
|
||||
// response.send('Welcome back, ' + request.session.username + '!');
|
||||
console.log(request.session.username, 'open main page');
|
||||
// Render home page with username
|
||||
response.render('home', { username: request.session.username });
|
||||
} else {
|
||||
// response.send('Please login to view this page!');
|
||||
response.redirect('/login');
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/login', function (request, response) {
|
||||
/// Render login template with any message from query parameters
|
||||
|
@ -64,6 +80,7 @@ app.get('/login', function (request, response) {
|
|||
});
|
||||
|
||||
|
||||
|
||||
app.post('/auth', async function (request, response) {
|
||||
// Capture the input fields
|
||||
const { login, password } = request.body;
|
||||
|
@ -151,37 +168,99 @@ app.get('/logout', function (request, response) {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
// Serve the signup form
|
||||
app.get('/signup', function (request, response) {
|
||||
|
||||
// Check if signup is enabled
|
||||
if (!config.enableSignup) {
|
||||
return response.status(403).send(`
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Signup Disabled</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
|
||||
h1 { color: red; }
|
||||
button { padding: 10px 20px; font-size: 16px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Signup is currently disabled.</h1>
|
||||
<p>Please contact the adminstrator for assistance.</p>
|
||||
<button onclick="window.location.href='/'">Back to < POS Diagnostic Assist > </button>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
// Check if signup is enabled
|
||||
if (!config.enableSignup) {
|
||||
return response.status(403).send(`
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Signup Disabled</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
|
||||
h1 { color: red; }
|
||||
button { padding: 10px 20px; font-size: 16px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Signup is currently disabled.</h1>
|
||||
<p>Please contact the adminstrator for assistance.</p>
|
||||
<button onclick="window.location.href='/'">Back to < POS Diagnostic Assist > </button>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
|
||||
response.render('signup', { message: request.query.message || '' });
|
||||
});
|
||||
|
||||
response.render('signup', { message: request.query.message || '' });
|
||||
|
||||
app.get('/download', async (req, res) => {
|
||||
const filename = req.query.filename || 'default.xlsx'; // Get filename from query parameter
|
||||
console.log('got the filename', filename);
|
||||
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const worksheet = workbook.addWorksheet('Sheet1');
|
||||
|
||||
// Set column widths
|
||||
worksheet.columns = [
|
||||
{ width: 9 },
|
||||
{ width: 16 },
|
||||
{ width: 42 },
|
||||
{ width: 94 }
|
||||
];
|
||||
|
||||
// Add header row
|
||||
const headerRow = worksheet.addRow(['Модель', 'Сер.номер', 'Заявленная неисправность', 'Произведенная работа']);
|
||||
|
||||
// Define border style
|
||||
const thinBlackBorder = {
|
||||
top: { style: 'thin', color: { argb: 'FF000000' } },
|
||||
left: { style: 'thin', color: { argb: 'FF000000' } },
|
||||
bottom: { style: 'thin', color: { argb: 'FF000000' } },
|
||||
right: { style: 'thin', color: { argb: 'FF000000' } }
|
||||
};
|
||||
|
||||
// Style the header row
|
||||
headerRow.eachCell({ includeEmpty: true }, (cell, colNumber) => {
|
||||
cell.fill = {
|
||||
type: 'pattern',
|
||||
pattern: 'solid',
|
||||
fgColor: { argb: '92D050' }
|
||||
};
|
||||
cell.font = {
|
||||
name: 'Calibri',
|
||||
size: 11,
|
||||
color: { argb: '000000' },
|
||||
bold: false
|
||||
};
|
||||
cell.alignment = { vertical: 'middle', horizontal: 'center' };
|
||||
cell.border = thinBlackBorder;
|
||||
});
|
||||
|
||||
// Style the rest of the cells with border
|
||||
worksheet.eachRow({ includeEmpty: true }, (row) => {
|
||||
row.eachCell({ includeEmpty: true }, (cell) => {
|
||||
if (!cell.border) { // Apply border only if not already defined
|
||||
cell.border = thinBlackBorder;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Generate buffer from workbook
|
||||
const buffer = await workbook.xlsx.writeBuffer();
|
||||
|
||||
// Set headers and send file
|
||||
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}.xlsx`);
|
||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
res.send(buffer);
|
||||
});
|
||||
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.info(`Listening on http://0.0.0.0:${PORT}/`);
|
||||
});
|
Loading…
Reference in New Issue