-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (70 loc) · 2.1 KB
/
server.js
File metadata and controls
80 lines (70 loc) · 2.1 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
71
72
73
74
75
76
77
78
79
80
var http = require('http'),
https = require('https'),
url = require("url"),
path = require("path"),
spawn = require("child_process").spawn,
jsonline = require('json-line-protocol').JsonLineProtocol,
cfg = require('./config'),
fs = require("fs");
http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname;
console.log("Requested uri: " + uri);
if ( uri == '/stream' ) {
var jsonTwitter = new jsonline();
var username = cfg.twitter_username,
password = cfg.twitter_password;
var options = {
host: 'stream.twitter.com',
port: 443,
path: '/1/statuses/filter.json?locations=-180,-90,180,90',
headers: {
'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64')
}
};
res.writeHead(200, {'Content-Type': 'text/event-stream'});
https.get(options, function(resp){
resp.on('data', function(chunk){
jsonTwitter.feed(chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
console.log("Got error: " + e);
});
jsonTwitter.on('value', function (value) {
res.write("event: twitter\n");
res.write("data: "+JSON.stringify(value)+"\n\n");
});
} else {
if ( uri == '/' ) uri = '/index.html';
var filename = path.normalize( path.join(process.cwd(), uri) );
console.log(filename);
if (filename.indexOf(__dirname) == 0 ) {
path.exists(filename, function(exists) {
if(!exists) {
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
res.end();
return;
}
fs.readFile(filename, "binary", function(err, file) {
if(err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
res.end();
return;
}
res.writeHead(200);
res.write(file, "binary");
res.end();
});
});
} else {
console.log("invalid path: " + filename);
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
res.end();
return;
}
}
}).listen(cfg.port, cfg.address);
console.log('Server running at http://'+cfg.address+':'+cfg.port+'/');