Created
July 2, 2012 20:28
-
-
Save jeffsu/3035507 to your computer and use it in GitHub Desktop.
Node Blog Examples
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
var connect = require('connect'); | |
var users = require('./lib/users'); | |
var app = connect.createServer(); | |
var redis = require('redis').createClient(); | |
var userCache = {}; | |
function getUser(id, cb) { | |
if (id in userCache) { | |
cb(userCache[id]); | |
} else { | |
users.get(id, cb); | |
} | |
} | |
redis.subscribe('update-user', function (id) { delete userCache[id] }); | |
app.get('/users/:userId', function (req, res) { | |
getUser(req.params.userId, function (user) { | |
res.end("Welcome " + user.name); | |
}); | |
}); |
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
var connect = require('connect'); | |
var users = require('./lib/users'); | |
var app = connect.createServer(); | |
var userCache = {}; | |
function getUser(id, cb) { | |
if (id in userCache) { | |
cb(userCache[id]); | |
} else { | |
users.get(id, cb); | |
} | |
} | |
app.get('/users/:userId', function (req, res) { | |
getUser(req.params.userId, function (user) { | |
res.end("Welcome " + user.name); | |
}); | |
}); |
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
var connect = require('connect'); | |
var users = require('./lib/users'); | |
var app = connect.createServer(); | |
var userCache = {}; | |
var counts = {}; | |
function getUser(id, cb) { | |
if (id in userCache) { | |
cb(userCache[id]); | |
} else { | |
users.get(id, cb); | |
} | |
} | |
app.get('/users/:userId', function (req, res) { | |
getUser(req.params.userId, function (user) { | |
if (req.url in counts) { | |
counts[req.url]++; | |
} else { | |
counts[req.url] = 1; | |
} | |
res.end("Welcome " + user.name); | |
}); | |
}); |
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
var connect = require('connect'); | |
var users = require('./lib/users'); | |
var app = connect.createServer(); | |
app.get('/users/:userId', function (req, res) { | |
users.get(req.params.userId, function (err, user) { | |
res.end("Welcome " + user.name); | |
}); | |
}); | |
app.get('/stats', function (req, res) { | |
res.end(JSON.stringify(counts)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment