-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2d609ad
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
modified from | ||
http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var sys = require("sys"), | ||
http = require("http"), | ||
url = require("url"), | ||
path = require("path"), | ||
fs = require("fs"); | ||
|
||
http.createServer(function(request, response) { | ||
var uri = url.parse(request.url).pathname; | ||
var filename = path.join(process.cwd(), uri); | ||
path.exists(filename, function(exists) { | ||
if(!exists) { | ||
response.writeHeader(404, {"Content-Type": "text/plain"}); | ||
response.write("404 Not Found\n"); | ||
response.end(); | ||
return; | ||
} | ||
|
||
fs.readFile(filename, "binary", function(err, file) { | ||
if(err) { | ||
response.writeHeader(500, {"Content-Type": "text/plain"}); | ||
response.write(err + "\n"); | ||
response.end(); | ||
return; | ||
} | ||
|
||
response.writeHeader(200); | ||
response.write(file, "binary"); | ||
response.end(); | ||
}); | ||
}); | ||
}).listen(8000); | ||
|
||
sys.puts("Server running at http://localhost:8080/"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Tweet Streamer</title> | ||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | ||
</head> | ||
<body> | ||
<ul id="tweets"></ul> | ||
<script type="text/javascript"> | ||
var tweet_list = $("#tweets"); | ||
|
||
function load_tweets() { | ||
$.getJSON("http://127.0.0.1:8000/stream", function(tweets) { | ||
|
||
$.each(tweets, function() { | ||
$("<li>").html(this.text).prependTo(tweet_list); | ||
}); | ||
load_tweets(); | ||
}); | ||
} | ||
|
||
setTimeout(load_tweets, 1000); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var sys = require("sys"), | ||
http = require("http"); | ||
|
||
http.createServer(function(req, res){ | ||
res.writeHead(200, {"Content-Type": "text/html"}); | ||
res.write("王懿"); | ||
res.end(); | ||
}).listen(8000); | ||
|
||
sys.puts("Server running at port 8000"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
var sys = require("sys"), | ||
http = require("http"), | ||
url = require("url"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
events = require("events"); | ||
|
||
function load_static_file(uri, response) { | ||
var filename = path.join(process.cwd(), uri); | ||
path.exists(filename, function(exists) { | ||
if(!exists) { | ||
response.writeHeader(404, {"Content-Type": "text/plain"}); | ||
response.end("404 Not Found\n"); | ||
return; | ||
} | ||
|
||
fs.readFile(filename, "binary", function(err, file) { | ||
if(err) { | ||
response.writeHeader(500, {"Content-Type": "text/plain"}); | ||
response.end(err + "\n"); | ||
return; | ||
} | ||
response.writeHeader(200); | ||
response.end(file, "binary"); | ||
}); | ||
}); | ||
} | ||
|
||
var twitter_client = http.createClient(80,"api.twitter.com"); | ||
var tweet_emitter = new events.EventEmitter(); | ||
function get_tweets(){ | ||
var req = twitter_client.request("GET","/1/statuses/public_timeline.json", {"host":"api.twitter.com"}); | ||
req.addListener("response",function(res){ | ||
var body = ""; | ||
res.addListener("data",function(data){ | ||
body += data; | ||
}); | ||
res.addListener("end",function(){ | ||
var tweets = JSON.parse(body); | ||
if(tweets.length > 0){ | ||
tweet_emitter.emit("tweets", tweets); | ||
} | ||
}); | ||
}); | ||
req.end(); | ||
} | ||
setInterval(get_tweets, 5000); | ||
|
||
http.createServer(function(req, res){ | ||
|
||
var uri = url.parse(req.url).pathname; | ||
sys.puts("uri = " + uri); | ||
|
||
if(uri == "/stream"){ | ||
var listener = tweet_emitter.addListener("tweets", function(tweets){ | ||
res.writeHeader(200, { "Content-Type" : "text/plain"}); | ||
res.end(JSON.stringify(tweets)); | ||
clearTimeout(timeout); | ||
}); | ||
|
||
var timeout = setTimeout(function(){ | ||
res.writeHeader(200, {"Content-Type":"text/plain"}); | ||
res.end(JSON.stringify([])); | ||
tweet_emitter.removeListener( listener); | ||
}, 10000); | ||
}else{ | ||
load_static_file(uri, res); | ||
} | ||
}).listen(8000); | ||
sys.puts("Server running at port 8000"); |