Skip to content

Commit

Permalink
Updated "Node + Redis = Fun" to work with v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilm authored and creationix committed Sep 7, 2010
1 parent 2b874ec commit 7c57c70
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
10 changes: 5 additions & 5 deletions articles/node-redis-fun.markdown
@@ -1,7 +1,7 @@
Title: Node + Redis = Fun
Author: Nikhil Marathe
Date: Tue Feb 23 2010 21:20:20 GMT+530 (IST)
Node: v0.1.91
Date: Mon Sep 06 2010 12:48:20 GMT+530 (IST)
Node: v0.2.0

node brings asynchronous, evented I/O to the server. Redis gives you a blazing fast database with support for strings, lists and sets. Both Redis and Node.js follow certain patterns, Redis for data-storage, and node for event based programming. I hope to give an introduction to both in this article. By the time we are done, we will have built a [Pastebin][] service.

Expand All @@ -13,7 +13,7 @@ I will assume that the reader is comfortable with Javascript, including using ev

Before we get down to the code, here is the software you will need:

* [node][] ( we will use [v0.1.91](http://github.com/ry/node/downloads) )
* [node][] ( we will use [v0.2.0](http://github.com/ry/node/downloads) )
* [Redis][]
* [redis-node-client][] - to connect to Redis from node. Already bundled within snip.
* [nerve][] - A micro-framework to handle routing. Use the bundled version which works with node v0.1.91.
Expand Down Expand Up @@ -175,12 +175,12 @@ It would have been even cooler if redis-node-client supported streaming the data
"-f", "html",
"-O", "full,style=pastie",
"-P", "title=Snippet #" + id ] );
pyg.stdout.addListener( "data", function( coloured ) {
pyg.stdout.on( "data", function( coloured ) {
if( coloured )
res.write( coloured );
} );

pyg.addListener( 'exit', function() {
pyg.on( 'exit', function() {
res.end();
});

Expand Down
45 changes: 31 additions & 14 deletions articles/node-redis-fun/snip.js
Expand Up @@ -5,6 +5,29 @@ var redis = require('redis-client')
, cp = require('child_process')
, _ = require('underscore')._;

/* Passes cb to a new instance redis.Client.connect
* but handles error in connecting
* accepts optional errback as second argument
* the callback gets a this.redis representing the redis object
*
* Returns nothing
*/
var withRedis = function( cb ) {
var errback = arguments[1];

var r = redis.createClient();

r.stream.on( 'connect', _.bind( cb, { redis : r } ) );

r.stream.on( "end", function(error) {
if( error ) {
process.stdio.writeError( "Error connecting to Redis database\n" );
if( typeof(errback) === "function" )
errback();
}
});
}

var languages =
[["apacheconf", "ApacheConf"],
["applescript", "AppleScript"],
Expand Down Expand Up @@ -145,7 +168,6 @@ var genLanguageList = function() {
} );
}

//formHtml
var formHtml = '<form action="/add" method="post">'
+ '<label for="code">Paste code</label><br>'
+ '<textarea name="code" rows="25" cols="80"></textarea><br>'
Expand All @@ -155,24 +177,22 @@ var formHtml = '<form action="/add" method="post">'
+ '</select>'
+ '<input type="submit" value="Paste!" /></form>';

//getPostParams
var getPostParams = function(req, callback){
var body = '';
req.addListener('data', function(chunk){
req.on('data', function(chunk){
body += chunk;
})
.addListener('end', function() {
.on('end', function() {
var obj = qs.parse( body.replace( /\+/g, ' ' ) ) ;
callback( obj );
});
}

//addSnippet
var addSnippet = function( req, res ) {
getPostParams( req, function( obj ) {
var r = redis.createClient();

r.stream.addListener( 'connect', function() {
r.stream.on( 'connect', function() {
r.incr( 'nextid' , function( err, id ) {
r.set( 'snippet:'+id, JSON.stringify( obj ), function() {
var msg = 'The snippet has been saved at <a href="/'+id+'">'+req.headers.host+'/'+id+'</a>';
Expand All @@ -183,19 +203,18 @@ var addSnippet = function( req, res ) {
});
};

//showSnippet
var showSnippet = function( req, res, id ) {
var r = redis.createClient();
r.stream.addListener( 'connect', function() {
r.stream.on( 'connect', function() {
r.get( 'snippet:'+id, function( err, data ) {
if( !data ) {
res.sendHeader( 404 );
res.writeHead( 404 );
res.write( "No such snippet" );
res.end();
return;
}

res.sendHeader( 200, { "Content-Type" : "text/html" } );
res.writeHead( 200, { "Content-Type" : "text/html" } );

var obj = JSON.parse( data.toString() );
var shortcode = languages.filter( function(el) {
Expand All @@ -207,12 +226,12 @@ var showSnippet = function( req, res, id ) {
"-f", "html",
"-O", "full,style=pastie",
"-P", "title=Snippet #" + id ] );
pyg.stdout.addListener( "data", function( coloured ) {
pyg.stdout.on( "data", function( coloured ) {
if( coloured )
res.write( coloured );
} );

pyg.addListener( 'exit', function() {
pyg.on( 'exit', function() {
res.end();
});

Expand All @@ -224,8 +243,6 @@ var showSnippet = function( req, res, id ) {
});
}

//create

nerve.create( [
[ /^\/([0-9]+)/, showSnippet ],
[ nerve.post("/add"), addSnippet ],
Expand Down
2 changes: 1 addition & 1 deletion authors/Nikhil Marathe.markdown
Expand Up @@ -3,6 +3,6 @@ Bitbucket: nikhilm
Email: nsm.nikhil@gmail.com
Homepage: http://kodeclutz.blogspot.com

I am a second year ( sophomore ) student at DA-IICT Gandhinagar in India. Currently I contribute to [KDE](http://www.kde.org) besides experimenting with *lots* of different languages and open source technologies. At this point I haven't decided what area of computers I am really interested in and so I work at all levels, from system calls to web applications.
I am a third year student at DA-IICT Gandhinagar in India. Currently I contribute to [KDE](http://www.kde.org) besides experimenting with *lots* of different languages and open source technologies. At this point I haven't decided what area of computers I am really interested in and so I work at all levels, from system calls to web applications.

I spend the rest of the day playing football, reading and enjoying college life. Willing to work for food :).

0 comments on commit 7c57c70

Please sign in to comment.