HLJ 发布于
2018-08-21 17:49:14

Node.js请求MySQL数据库输出表里的内容

var express   =    require("express");
var mysql     =    require('mysql');
var app       =    express();
var pool      =    mysql.createPool({
    connectionLimit : 100, //important
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'vue',
    debug    :  false
});
function handle_database(req,res) {
    pool.getConnection(function(err,connection){
        if (err) {
          res.json({"code" : 100, "status" : "Error in connection database"});
          return;
        }   
        console.log('connected as id ' + connection.threadId);
        connection.query("select * from tab1",function(err,rows){
            connection.release();
            if(!err) {
                res.json(rows);
            }           
        });
        connection.on('error', function(err) {      
              res.json({"code" : 100, "status" : "Error in connection database"});
              return;     
        });
  });
}
app.get("/",function(req,res){-
        handle_database(req,res);
});
app.listen(3000);
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-08-21/193.html
最后生成于 2023-06-18 18:38:31
此内容有帮助 ?
0