-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 1.3 KB
/
index.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
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var request = require("request");
var TOKEN_ID = process.env.TOKEN_ID;
var TOKEN_KEY = process.env.TOKEN_KEY;
var server = http.createServer(function(req, res) {
var page = url.parse(req.url).pathname;
console.log(page);
res.writeHead(200, {"Content-Type": "text/plain"});
if (page == '/') {
res.write('REST API NodeJS IOT OVH - [email protected]');
}
else if (page == '/send') {
var params = querystring.parse(url.parse(req.url).query);
var measurement = params['measurement'];
var value = Number(params['value']);
var location = params['location'];
var data = [{
metric:measurement,
value:value,
tags:{
source:"RestNodeJS-API",
region:location
}
}];
// Send data
request({
uri: "https://opentsdb-gra1.tsaas.ovh.com/api/put",
auth: {
user: TOKEN_ID,
pass: TOKEN_KEY,
sendImmediately: true
},
method: "POST",
json: data
}, function (error, response, body) {
if (error) {
console.log(error);
} else if (response.statusCode >= 400) {
console.log(body);
console.log(response.statusMessage);
} else {
console.log("Success!" + location + " / " + measurement + " / " +value +" ");
}
});
res.write('Success');
}
res.end();
});
server.listen(81);