Untitled

 avatar
unknown
plain_text
a year ago
4.7 kB
8
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('/', (req, res) => {
    res.sendFile(path.join(templatePath, 'template', 'index.html'));
});

// Serve the webdata.js file
app.get('/webdata.js', (req, res) => {
    res.sendFile(path.join(templatePath, 'template', 'webdata.js'));
});

// Serve the style.css file
app.get('/assets/css/style.css', (req, res) => {
    res.sendFile(path.join(templatePath, 'template', 'assets', 'css', 'style.css'));
});

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) => {
    console.log('Data request received:', req.query); // Log the request query parameters

    const { page, limit, search, patientID, timestamp } = req.query;
    const itemCount = req.query.itemCount || 20; // Default to 20 items per page
    const offset = (page - 1) * itemCount; // Calculate offset

    let query = 'SELECT patientId, operation_name AS operationName, min_value AS minValue, max_value AS `maxValue`, patient_value AS patientValue, compared_value AS comparedValue, units, timestampOperation AS timestamp FROM patient_operations';
    let queryParams = [];

    // Add search filter to the query if search parameter is provided
    if (search) {
        query += ' WHERE patientId LIKE ? OR timestampOperation LIKE ?';
        queryParams.push(`%${search}%`, `%${search}%`); // Use wildcard to match any substring
    }

    // Add patientID filter to the query if patientID parameter is provided
    if (patientID) {
        query += (search ? ' AND' : ' WHERE') + ' patientId = ?';
        queryParams.push(patientID);
    }

    // Add timestamp filter to the query if timestamp parameter is provided
    if (timestamp) {
        query += (search || patientID ? ' AND' : ' WHERE') + ' timestampOperation = ?';
        queryParams.push(timestamp);
    }

    // Add pagination to the query
    query += ' LIMIT ? OFFSET ?';
    queryParams.push(parseInt(itemCount), offset);

    // Execute the SQL query with parameters
    console.log('Executing SQL query:', query, queryParams); // Log the SQL query and parameters
    pool.query(query, queryParams, (error, results) => {
        if (error) {
            console.error('Error querying MySQL:', error);
            res.status(500).json({ error: 'Internal server error' });
            return;
        }

        // Log the retrieved data
        console.log('Retrieved data:', results);

        // Send the data back to the frontend
        console.log('Sending data to frontend:', results); // Log data being sent to frontend
        res.send(renderData(results)); // Render the data before sending it to the frontend
    });
});

// Function to render data in HTML format
function renderData(data) {
    let html = '<table class="table">';
    html += '<thead><tr><th>Patient ID</th><th>Operation Name</th><th>Min Value</th><th>Max Value</th><th>Patient Value</th><th>Compared Value</th><th>Units</th><th>Timestamp</th></tr></thead>';
    html += '<tbody>';
    data.forEach(row => {
        html += `<tr><td>${row.patientId}</td><td>${row.operationName}</td><td>${row.minValue}</td><td>${row.maxValue}</td><td>${row.patientValue}</td><td>${row.comparedValue}</td><td>${row.units}</td><td>${row.timestamp}</td></tr>`;
    });
    html += '</tbody></table>';
    return html;
}

// 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