Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
5.3 kB
6
Indexable
Never
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'));
});

// 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')));

// Define the renderData function to format the data before sending it to the frontend
function renderData(data) {
    // Assuming data is an array of objects, you can stringify it for sending to the frontend
    return JSON.stringify(data);
}

// Route to handle data requests from frontend with pagination, item count, and search
// Route to handle data requests from frontend without pagination in backend
// Route to handle data requests from frontend with pagination, item count, and search
app.get('/data', (req, res) => {
    // Extract query parameters
    const { search, 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 
        FROM 
            patient_operations po 
        INNER JOIN 
            pacienti p 
        ON 
            po.patientId = p.patientID
    `;
    let queryParams = [];

    // Add search filter to the query if search parameter is provided
    if (search) {
        query += ' WHERE po.patientId LIKE ? OR po.timestampOperation LIKE ? OR p.Meno LIKE ?';
        queryParams.push(`%${search}%`, `%${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') + ' po.patientId = ?';
        queryParams.push(patientID);
    }

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

    // Add pagination limits to the query
    query += ' 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 a separate SQL query to fetch the total count of records
        let countQuery = 'SELECT COUNT(*) AS totalCount FROM patient_operations';
        
        // Execute the count query
        pool.query(countQuery, (countError, countResult) => {
            if (countError) {
                console.error('Error querying record count from MySQL:', countError);
                res.status(500).json({ error: 'Internal server error' });
                return;
            }
            
            // Retrieve total count
            const totalCount = countResult[0].totalCount;
            // Calculate total pages
            const totalPages = Math.ceil(totalCount / pageSize);
        
            // Send both the data and pagination information back to the frontend
            res.json({ data: results, totalResults: totalCount, totalPages });
        });
        });
    });

// Start the server
const PORT = process.env.PORT || 3500;
app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
});
Leave a Comment