- 我正在使用异步响应来获取输出
- 我正在使用快递
- 我还将两个表数据组合成单个 JSON 响应
快递程序::
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'xxx',
password: "xxx",
database: 'test123'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 8084);
app.get('/',function(request,response){
var first, second;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM test1', function(err, rows, fields)
{
console.log('Connection result error '+err);
first = JSON.stringify(rows);
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM test2', function(err, rows, fields)
{
console.log('Connection result error '+err);
second = JSON.stringify(rows);
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'first' : first,
'second' : second
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我需要在这里进行哪些代码更改