-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
64 lines (56 loc) · 1.49 KB
/
helper.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
var http = require("http");
var https = require("https");
/**
* getJSON: REST get request returning JSON object(s)
* @param options: http options object
* @param callback: callback to pass the results JSON object(s) back
*/
exports.getJSON = function(options, onResult)
{
var prot = options.port == 443 ? https : http;
var req = prot.request(options, function(res)
{
var output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', function(err) {
//res.send('error: ' + err.message);
});
req.end();
};
//POST
exports.putJSON = function(options, data, onResult)
{
var prot = options.port == 443 ? https : http;
var req = prot.request(options, function(res)
{
var output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', function(err) {
//res.send('error: ' + err.message);
});
//post here
req.write(data);
//
req.end();
};
exports.mylog = function (log) {
var ts = new Date().toLocaleTimeString();
//console.log(ts + " " + log.toString());
console.log(log);
};