user2609157
user2609157

Reputation:

JSON response using express

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

Here

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

Upvotes: 0

Views: 6397

Answers (1)

fakewaffle
fakewaffle

Reputation: 3081

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

Upvotes: 1

Related Questions