-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (113 loc) · 3.11 KB
/
server.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var express = require('express');
var qs = require("querystring");
var _mysql = require('mysql');
var url = require("url");
var fs = require("fs");
var app = express();
var server = app.listen(3000);
var strJson;
var resultQuery;
var mysql = _mysql.createConnection({
//host
host: '127.0.0.1',
//user
user: 'root',
//password
password: '689468',
//port
port: 3306,
//the name of database
database: 'datav'
});
var _sql_rain = "SELECT rating, comment from `comments` ORDER by 'id' desc limit 60";
//var _sql_pie = ;
//http://www.cnblogs.com/jiaxiaoai/archive/2011/06/27/2091382.html
//app.get('/public/connectDB', function(req, res) {
//var sInfo = qs.parse(url.parse(req.url).query);
mysql.query(_sql_rain, function(error, results){
if(error){
console.log(error);
//res.end();
}
else{
strJson = JSON.stringify(results);
//console.log("query succeed..." +strJson);
// res.write(strJson);
fs.writeFile('./public/rain.json', strJson, function (err) {
if(err) {
console.error(err);
} else {
console.log('Write in successfully---');
}
});
//res.end();
}
});
mysql.end(function (err) {
if (err) {
return;
}
console.log("mysql close succeed...");
});
app.use(express.static('public'));
console.log("My socket server is running");
app.get('/public/Barfall', function(req, res) {
var sInfo = qs.parse(url.parse(req.url).query);
var _mysql2 = require('mysql');
var mysql2 = _mysql2.createConnection({
//host
host: '127.0.0.1',
//user
user: 'root',
//password
password: '689468',
//port
port: 3306,
//the name of database
database: 'datav'
});
mysql2.query(_sql_rain, function(error, results){
if(error){
console.log(error);
}
else{
strJson = JSON.stringify(results);
console.log("query succeed...");
res.write(strJson);
res.end();///very important point
}
});
mysql2.end(function (err) {
if (err) {
return;
}
console.log("mysql2 close succeed...");
});
});
// //create a connection
// mysql.connect(function (err) {
// if (err) {
// console.log('connect-' + err);
// }
// console.log('connect succeed...');
// });
//
//var arr = [];
// mysql.query(_sql, function (err, rows) {
// if (err) {
// console.log("query-" + err);
// }
// console.log("query succeed..." + rows);
// //resultQuery = results;
// for (var i = 0; i < rows.length; i++) {
// arr[i] = rows[i].name;
// }
// });
//
//关闭连接
// mysql.end(function (err) {
// if (err) {
// return;
// }
// console.log("close succeed...");
// });