-
Notifications
You must be signed in to change notification settings - Fork 28
/
s5_send_meangful_graph_html.js
55 lines (47 loc) · 1.38 KB
/
s5_send_meangful_graph_html.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
/*
* 2018/3/23 Kyuho Kim
* ekyuho@gmail.com
* GET으로 호출하는 경우.
* http://localhost:8082/graph
*/
var express = require('express')
var app = express()
fs = require('fs');
mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'mypassword',
database: 'mydb'
})
connection.connect();
app.get('/graph', function (req, res) {
console.log('got app.get(graph)');
var html = fs.readFile('./graph.html', function (err, html) {
html = " "+ html
console.log('read file');
var qstr = 'select * from sensors ';
connection.query(qstr, function(err, rows, cols) {
if (err) throw err;
var data = "";
var comma = ""
for (var i=0; i< rows.length; i++) {
r = rows[i];
data += comma + "[new Date(2017,04-1,"+ r.id +",00,38),"+ r.value +"]";
comma = ",";
}
var header = "data.addColumn('date', 'Date/Time');"
header += "data.addColumn('number', 'Temp');"
html = html.replace("<%HEADER%>", header);
html = html.replace("<%DATA%>", data);
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(html);
res.end();
});
});
})
var server = app.listen(8082, function () {
var host = server.address().address
var port = server.address().port
console.log('listening at http://%s:%s', host, port)
});