Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.69 KB
/
Copy pathindex.js
File metadata and controls
70 lines (59 loc) · 1.69 KB
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
var http, director, cool, bot, router, server, port, querystring;
var HTTPS = require('https');
http = require('http');
director = require('director');
cool = require('cool-ascii-faces');
querystring = require('querystring');
bot = require('./bot.js');
router = new director.http.Router({
'/' : {
post: bot.respond,
get: ping
}
});
server = http.createServer(function (req, res) {
req.chunks = [];
req.on('data', function (chunk) {
req.chunks.push(chunk.toString());
});
router.dispatch(req, res, function(err) {
res.writeHead(err.status, {"Content-Type": "text/plain"});
res.end(err.message);
});
});
port = Number(process.env.PORT || 5000);
server.listen(port);
function ping() {
this.res.writeHead(200);
this.res.end("Hey, I'm Cool Guy.");
}
function apiRequest(endpoint, body) {
var options, body, coinReq;
var p = endpoint + '?' + querystring.stringify(body);
options = {
hostname: 'min-api.cryptocompare.com',
path: p,
method: 'GET'
};
coinReq = HTTPS.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
console.log("end");
var responseObject = JSON.parse(responseString);
return responseObject;
});
})
coinReq.on('error', function(err) {
console.log('error posting message ' + JSON.stringify(err));
return JSON.parse({'error': 'Something went wrong.'});
});
coinReq.on('timeout', function(err) {
console.log('timeout posting message ' + JSON.stringify(err));
return JSON.parse({'error': 'API timed out'});
});
coinReq.end();
}