Created
May 14, 2012 01:50
-
-
Save gavinmyers/2691244 to your computer and use it in GitHub Desktop.
Coffeescript, Nodejs, Redis Hello World
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
http = require "http" | |
redis = require "redis" | |
Single = require("./single") | |
http.createServer( (req, res) -> | |
client = redis.createClient() | |
people = [] | |
client.get "people", (err,cres) -> | |
people = if cres == null then [] else JSON.parse cres | |
people.push new Single 'Mary', Math.floor(Math.random() * 100), Math.floor(Math.random() * 10) < 8 | |
people.push new Single 'Red', Math.floor(Math.random() * 100), Math.floor(Math.random() * 10) < 6 | |
client.set "people", JSON.stringify(people), redis.print | |
console.log client.get "people" | |
console.log JSON.stringify(people) | |
msg = "<h1>GOOD MORNING...</h1>" | |
for person in people | |
msg += "<hr/>Hello, <b>#{person.name}</b>, how are you today?<br/>I hear you are #{person.age} year(s) old.<br/>" | |
msg += if person.dead then "I'm sorry to hear about your... <span style='color:red'>death</span>!" else "Glad to know that you are alive!" | |
msg += "<h1>THAT IS ALL FOR NOW</h1>" | |
res.writeHead 200, "Content-Type": "text/html" | |
res.end msg | |
).listen 8000 | |
console.log 'Server running at http://127.0.0.1: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
module.exports = class Single | |
constructor: (@name, @age, @dead) -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment