Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 9th, 2012  |  syntax: JavaScript  |  size: 2.33 KB  |  views: 595  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var sys     =  require('sys'),
  2.     http    =  require('http'),
  3.     url     =  require('url'),
  4.     path    =  require('path'),
  5.     fs      =  require('fs'),
  6.     events  =  require('events');
  7.  
  8. function load_static_file( uri, response ) {
  9.   var filename = path.join( process.cwd(), uri );
  10.  
  11.   path.exists( filename, function( exists ) {
  12.    
  13.     if( !exists ) {
  14.       response.setHeader('Content-Type', 'text/plain');
  15.       response.statusCode = 404;
  16.       response.write('404 Not Found\n');
  17.       response.end();
  18.       return;
  19.     }
  20.  
  21.     fs.readFile( filename, 'binary', function( err, file ) {
  22.      
  23.       if( err ) {
  24.         response.setHeader( 'Content-Type', 'text/plain' );
  25.         response.statusCode = 500;
  26.         response.write( err, + '\n' );
  27.         response.end();
  28.         return;
  29.       }
  30.  
  31.       response.statusCode = 200;
  32.       response.write( file, 'binary' );
  33.       response.end();
  34.  
  35.     });
  36.  
  37.   });
  38. }
  39.  
  40.  
  41. var tweet_emitter  = new events.EventEmitter();
  42.  
  43. function get_tweets() {
  44.  
  45.   var request = http.request({
  46.     method:'GET',
  47.     port:80,
  48.     path:'/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=sam3k_&count=2',
  49.     hostname:'api.twitter.com'
  50.   });
  51.  
  52.   request.addListener( 'response', function(response) {
  53.     var body = '';
  54.    
  55.     response.addListener( 'data', function(data) {
  56.       body += data;
  57.     });
  58.  
  59.     response.addListener( 'end', function() {
  60.       var tweets = JSON.parse(body);
  61.       if( tweets.length > 0) {
  62.         tweet_emitter.emit('tweets', tweets);
  63.       }
  64.     });
  65.  
  66.   });
  67.  
  68.   request.end();
  69. }
  70.  
  71. setInterval( get_tweets, 5000 );
  72.  
  73. http.createServer(function(request, response) {
  74.   var uri = url.parse(request.url).pathname;
  75.   if(uri === '/stream') {
  76.    
  77.     var listener = tweet_emitter.addListener('tweets', function(tweets) {      
  78.       response.statusCode = 200;
  79.       response.write(JSON.stringify(tweets));
  80.       response.end();
  81.       clearTimeout(timeout);
  82.     });
  83.  
  84.     var timeout = setTimeout(function(){
  85.       response.setHeader('Content-Type', 'text/plain');
  86.       response.write(JSON.stringify([]));
  87.       response.end();
  88.       tweet_emitter.removeListener(listener);
  89.     },10000);
  90.  
  91.   } else {
  92.     load_static_file(uri, response);
  93.   }
  94. }).listen(8080);
  95.  
  96. sys.puts('Server Running at http://localhost:8080/');