使用express的JSON响应

时间:2013-08-16 09:54:00

标签: node.js express

我有一个来自Express的工作代码,它返回从服务器到客户端的JSON响应

这里

  • 当我使用http:://myserverip/时,我从Table1获得了一个JSON
  • 当我使用Http:://myserverip/table2时,我从Table2获得了一个JSON

据我了解,我需要发出2个单独的请求,以便从两个单独的表中获取JSON 但是有没有办法在一个JSON响应中一次从两个表中获取数据

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

感谢

1 个答案:

答案 0 :(得分:1)

我建议使用像async这样的库。而不是像askkirati建议的嵌套回调。

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