使用express显示节点mysql结果

时间:2016-09-12 12:10:57

标签: mysql node.js express

我正在尝试使用节点mysql显示多行,我坚持将结果导入路径。

查看下面的代码,我可以控制路线之外的所有结果,但它只会在路线中控制1个结果。

var port = process.env.PORT || 3000,
express = require('express'),
app = express();

app.set('view engine', 'ejs');

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'blablabla.com',
    user: 'user',
    password: 'password',
    database: 'database'
});

connection.connect(function(err) {
    if (err) {
        console.error('error connecting: ' + err.stack);
        return;
    }
    console.log('connected as id ' + connection.threadId);
});

/// start query
var query = connection.query('SELECT * FROM table LIMIT 3');
query
    .on('error', function(err) {
        console.log(err);
    })
    .on('result', function(row) {

        console.log(row); /// displays all 3 results

        app.get('/', function(req, res) {
            res.send(row); /// displays only 1 result
            console.log(row); /// displays only 1 result
        });
    });


/// end query
connection.end();
app.listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');

修改

经过大量的研究,我偶然发现了https://youtu.be/g-_l_v_uX6E?t=15m36s,这证明了我想要实现的目标,即路线中的多个结果。

var port = process.env.PORT || 3000,
express = require('express'),
app = express();

app.set('view engine', 'ejs');

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'blablabla.com',
  user     : 'user',
  password : 'pass',
  database : 'database'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

var units = null;

connection.query('SELECT * FROM table LIMIT 3', function(err, rows, fields){
      if(err) throw err;
      units = rows;

      connection.end();
      app.get('/', function(req, res) {
        res.send(units); /// multiple results to the browser
      });
});

app.listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');

1 个答案:

答案 0 :(得分:0)

如果我问你问题是否正确,你想在致电http://127.0.0.1...时获取数据库信息?

如果需要,您需要对代码进行一些小修改:

var port = process.env.PORT || 3000,
express = require('express'),
app = express();

app.set('view engine', 'ejs');

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'blablabla.com',
    user: 'user',
    password: 'password',
    database: 'database'
});


function getRows(res){
    connection.connect(function(err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return;
        }
        console.log('connected as id ' + connection.threadId);

        /// start query
        var query = connection.query('SELECT * FROM table LIMIT 3');
        query
            .on('error', function(err) {
                connection.end(); /// end query
                console.log(err);
            })
            .on('result', function(row) {

                res.send(row); /// displays only 1 result
                console.log(row); /// displays only 1 result
                connection.end(); /// end query
                console.log(row); /// displays all 3 results
                return;
            });
   });

}


app.get('/', function(req, res) {
            getRows(res);

});

app.listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');