-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnmysql2.js
46 lines (38 loc) · 1.12 KB
/
connmysql2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var express = require('express');
var mysql = require('mysql');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100,
host : 'localhost',
user : 'root',
password : '',
database : 'company',
debug : false
});
function handle_database(req, res) {
pool.getConnection(function(err, connection){
if(err)
{
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
var columns = ['id', 'contact_name', 'contact_tel', 'created_at'];
connection.query('SELECT ?? FROM ?? LIMIT 0,1000', [columns, 'company'], 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);