-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (63 loc) · 2.07 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
var http = require('http');
var port = process.env.PORT || 3000;
var ac = require('./index.js');
ac.import(function(err, count) {
console.log("Imported " + count.length + " Scrabble words!");
});
// update
var fs = require('fs');
var index = fs.readFileSync(__dirname + '/index.html');
var Twit = require('twit');
// require('../config.js');
var waitCount = 0;
var T = new Twit({
consumer_key: 'Kn4MIcuzclV9QhWCsetpz18zO',
consumer_secret: 'XU8c0aVdGmWj5ZwLGSgRCjpUujsfBrPGpaAOTFmp8L5g0H7CZl',
access_token: '2149662469-r0STzXowAMF2SDj4MHKobCSDmw7b43sMN6OUoQX',
access_token_secret: 'Vzpc9AxaYzW7olD8CSP48CL1LWZEL2GTCH6wxJGpCWTzJ'
});
http.createServer(function handler(request, response) {
var url = request.url;
if (url.length === 1) {
response.writeHead(200, {"Content-Type": "text/html"});
response.end(index.toString());
}
else if (url.indexOf('/twitter/') > -1) {
var searchTerm = url.split('/')[2].toString();
var tweetEmbed = '';
var tweet;
var tweetID;
var tweetCount = 0;
T.get('search/tweets', { q: searchTerm, count: 3}, function(err, data, response) {
for (var i = 0; i < 3; i++) {
tweet = data.statuses[i].text;
tweetID = data.statuses[i].id_str;
T.get('statuses/oembed', {id: tweetID, hide_media: true}, function(err, data, response) {
tweetEmbed += data.html;
tweetCount++;
});
}
});
setTimeout(function() {
{ response.end(tweetEmbed); }
}, 3000);
}
else if (url.indexOf('/words/') > -1) {
var scrab = url.split('/')[2];
var ok = ac.numWords(scrab.length, scrab).join(',');
if(ok.length === 0) {response.end("No results found!");}
response.end(ok);
}
else {
fs.readFile(__dirname + url, function(err, data){
if (err){
response.end();
} else {
var ext = url.split('.')[1];
response.writeHead(200, {'Content-Type' : 'text/' + ext});
response.end(data);
}
});
}
}).listen(port);
console.log('node http server listening on http://localhost:' + port);