Untitled
unknown
javascript
9 months ago
4.6 kB
5
Indexable
const express = require('express'); const mysql = require('mysql2'); const path = require('path'); const app = express(); // Your MySQL connection configuration const connectionConfig = { host: 'localhost', user: 'root', password: 'Aeondev555666', database: 'hl7_data' }; // Create a MySQL connection pool const pool = mysql.createPool(connectionConfig); // Define the directory where your HTML and JS files are located const templatePath = 'c:/Users/andre/Desktop/web/corona-free-dark-bootstrap-admin-template-1.0.0'; // Serve static files (CSS, JavaScript, images) from the 'assets' directory app.use(express.static(path.join(templatePath, 'assets'))); // Serve the index.html file app.get('/index.html', (req, res) => { res.sendFile(path.join(templatePath, 'template', 'index.html')); }); app.get('/assets/css/style.css', (req, res) => { res.sendFile(path.join(templatePath, 'template', 'assets', 'css', 'style.css')); }); // Serve other static files like CSS, JS, fonts, images, etc. app.use('/assets/fonts', express.static(path.join(templatePath, 'template', 'assets', 'fonts'))); app.use('/assets/images', express.static(path.join(templatePath, 'template', 'assets', 'images'))); app.use('/assets/js', express.static(path.join(templatePath, 'template', 'assets', 'js'))); app.use('/assets/scss', express.static(path.join(templatePath, 'template', 'assets', 'scss'))); app.use('/assets/vendors', express.static(path.join(templatePath, 'template', 'assets', 'vendors'))); // Route to handle data requests from frontend with pagination, item count, and search app.get('/data', (req, res) => { // Extract query parameters const { search, NAZKL, patientID, timestamp, page, limit } = req.query; // Pagination parameters const pageNumber = parseInt(page) || 1; // Default to page 1 if not provided const pageSize = parseInt(limit) || 10; // Default page size to 10 if not provided const offset = (pageNumber - 1) * pageSize; // Construct the main SQL query based on filters and pagination parameters let query = ` SELECT po.patientId, po.operation_name AS operationName, po.min_value AS minValue, po.max_value AS \`maxValue\`, po.patient_value AS patientValue, po.compared_value AS comparedValue, po.units, po.timestampOperation AS timestamp, p.Meno, k.NAZKL FROM patient_operations po INNER JOIN pacienti p ON po.patientId = p.patientID INNER JOIN klienti k ON p.id_kln = k.ID_ADR `; let queryParams = []; // Add NAZKL filter to the query if NAZKL parameter is provided if (NAZKL) { query += ' WHERE k.NAZKL LIKE ?'; queryParams.push(`%${NAZKL}%`); } // Add timestamp filter to the query if timestamp parameter is provided if (timestamp) { query += (NAZKL ? ' AND' : ' WHERE') + ' po.timestampOperation = ?'; queryParams.push(timestamp); } // Add pagination limits to the main query query += ' GROUP BY k.NAZKL, po.timestampOperation LIMIT ? OFFSET ?'; queryParams.push(pageSize, offset); // Execute the main SQL query to fetch data pool.query(query, queryParams, (error, results) => { if (error) { console.error('Error querying MySQL:', error); res.status(500).json({ error: 'Internal server error' }); return; } // Construct count query to get totalResults const countQuery = `SELECT COUNT(*) AS totalResults FROM patient_operations po INNER JOIN pacienti p ON po.patientId = p.patientID INNER JOIN klienti k ON p.id_kln = k.ID_ADR`; // Execute count query pool.query(countQuery, queryParams, (countError, countResult) => { if (countError) { console.error('Error querying MySQL for count:', countError); res.status(500).json({ error: 'Internal server error' }); return; } const totalResults = countResult[0].totalResults; // Send the data and totalResults count back to the frontend res.json({ data: results, totalResults }); }); }); }); // Start the server const PORT = process.env.PORT || 3500; app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
Editor is loading...
Leave a Comment