-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy paths3_web_server_insert_db.js
51 lines (43 loc) · 1.22 KB
/
s3_web_server_insert_db.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
/*
* 2020/3/23 Kyuho Kim
* GET으로 호출하는 경우.
* http://localhost:8080/log?device=202&unit=3&type=T&value=24.2&seq=34
*/
var express = require('express');
var app = express();
mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'mypassword',
database: 'mydb'
})
connection.connect();
function insert_sensor(device, unit, type, value, seq, ip) {
obj = {};
obj.seq = seq;
obj.device = device;
obj.unit = unit;
obj.type = type;
obj.value = value;
obj.ip = ip.replace(/^.*:/, '')
var query = connection.query('insert into sensors set ?', obj, function(err, rows, cols) {
if (err) throw err;
console.log("database insertion ok= %j", obj);
});
}
app.get('/', function(req, res) {
res.end('Nice to meet you');
});
app.get('/log', function(req, res) {
r = req.query;
console.log("GET %j", r);
insert_sensor(r.device, r.unit, r.type, r.value, r.seq, req.connection.remoteAddress);
res.end('OK:' + JSON.stringify(req.query));
});
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log('listening at http://%s:%s', host, port)
});