Friday, 14 April 2017

Pass data between Express routes in order to create two responses

I have a project where when the user presses the submit button it sends the the data 'to' and 'from' to '/charts' on my server, which is express.js. (Below is some of my pug script).

//index.pug    
form(action="/charts" method="get")
            select
                    option(value='chart') Chart
                    option(value='report') Report
            p From:
                    input(type='date' id='from' name='from')
            p To:
                    input(type='date' id='to' name='to')
            input(type="submit" value="Submit")

On the server side, I want to be able to use the data the user has just submitted in an SQL query, the result of which will be sent as JSON to my client-side JavaScript which creates some google charts visualizations.

//server.js
app.get('/charts', function(req, res, next) {

    var to = req.params.to;
    var from = req.params.from;

    console.log("Dates /dateRange " + to + " - " + from);

    //SQL Query
    connection.query("select date_, count(*) AS 'Total' from ( select date(insert_timestamp) date_ from test_table where api_task_type='hifd' AND insert_timestamp BETWEEN '" + from + "' AND '" + to + "')a group by date_ order by date_", function(err, rows, fields){
    if(!err){
    var table = [];
    for (var i in rows) {
            table.push({
                    date: String(rows[i].date_.getFullYear() + '-' + (rows[i].date_.getMonth() + 1) + "-" + rows[i].date_.getDate()),
                            Record_Count: (rows[i].Total),
                    });
            console.log(rows[i]);
    }
    console.log("The JSON data is: " + table);
    res.json(table);
    }
});

});

The problem I am having is that, along with sending the json data, I want to render my visualization page (chart.pug), but express doesnt allow you to send more than one response. So I would like something like:

app.get('/chartQuery', function(req, res, next) {

    var to = req.params.to;
    var from = req.params.from;

    console.log("Dates /dateRange " + to + " - " + from);

    //SQL Query
    connection.query("select date_, count(*) AS 'Total' from ( select date(insert_timestamp) date_ from test_table where api_task_type='hifd' AND insert_timestamp BETWEEN '" + from + "' AND '" + to + "')a group by date_ order by date_", function(err, rows, fields){
    if(!err){
    var table = [];
    for (var i in rows) {
            table.push({
                    date: String(rows[i].date_.getFullYear() + '-' + (rows[i].date_.getMonth() + 1) + "-" + rows[i].date_.getDate()),
                            Record_Count: (rows[i].Total),
                    });
            console.log(rows[i]);
    }
    var stringtable = JSON.stringify(table);
    console.log("The JSON data is: " + table);
    res.json(table);
    //Render the chart page
    res.render('chart');
    }
});

});

Any ideas how to achieve this? I want to send my json data extracted from the SQL query and render my page, all at once.



via AlanDev1989

No comments:

Post a Comment