0

I have a working code from Express that returns JSON response from the server to client

Here

  • when i use http:://myserverip/ I get a JSON from Table1
  • when i use Http:://myserverip/table2 I get a JSON from Table2

So as i understand i need to make 2 separate requests to get JSON from two separate tables But is there a way to get data from both the tables at a time in one JSON response

var express = require('express')
  , http = require('http')
  , mysql = require('mysql'); // <---- HERE

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: "root",
    database: 'DB'
});

connection.connect(); // <---- AND HERE

// all environments
app.set('port', process.env.PORT || 7002);


app.get('/',function(request,response){
connection.query('SELECT * FROM table1', function(err, rows, fields)

    {
            console.log('Connection result error '+err);
            console.log('no of records is '+rows.length);
                    response.writeHead(200, { 'Content-Type': 'application/json'});
            response.end(JSON.stringify(rows));
    });

} );


app.get('/table2',function(request,response){
connection.query('SELECT * FROM table2', function(err, rows, fields)

    {
            console.log('Connection result error '+err);
            console.log('no of records is '+rows.length);
                    response.writeHead(200, { 'Content-Type': 'application/json'});
            response.end(JSON.stringify(rows));
    });

} );



http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Thanks

2
  • Hello, Do you want to append results of two table in single request? Commented Aug 16, 2013 at 10:30
  • 2
    You could do this, do query for table2 inside callback of table1 and use response.json({table1:table1rowJSON, table2: table2rowJSON}); Commented Aug 16, 2013 at 10:35

1 Answer 1

1

I would suggest using a library like async. Instead of nested callbacks as askkirati suggested.

var express = require('express')
  , async = require('async')
  , http = require('http')
  , mysql = require('mysql'); // <---- HERE

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: "root",
    database: 'DB'
});

connection.connect(); // <---- AND HERE

// all environments
app.set('port', process.env.PORT || 7002);


app.get('/',function(request,response){
    var first, second;

    async.series( [

        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM table1', function(err, rows, fields)

                {
                        console.log('Connection result error '+err);
                        console.log('no of records is '+rows.length);
                        first = JSON.stringify(rows);

                        callback();
                });
        },

        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM table2', function(err, rows, fields)

            {
                    console.log('Connection result error '+err);
                    console.log('no of records is '+rows.length);
                    second = JSON.stringify(rows);

                    callback();
            });
        }

    // Send the response
    ], function ( error, results ) {
        response.writeHead(200, { 'Content-Type': 'application/json'});
        response.end({
            'first' : first,
            'second' : second
        });
    } );

} );

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
Sign up to request clarification or add additional context in comments.

1 Comment

You may want to use async.parallel, so the second SELECT doesn't have to wait for the first to finish.