Archive for March, 2011

Express with Jade on node.js

Over the weekend, I was playing around with node.js – a javascript implementation of an event I/O handler. Node.js is an event based framework that allows developer to easily build scalable network programs. The framework runs on the javascript V8 engine and was influenced by by Ruby’s Event Machine and Python’s Twisted. Node.js breaks the barrier between server side language and client side language.

Node.js is very easy to use, node.js came with a neat package manager like python’s easy_install and ruby’s gem. The packages that I played around with was express – a web framework, and Jade– a template engine. Below, I will show you how to combine the two packages Jade and Express to make a web app in 5 minutes.
first, make sure you have node.js, npm, express and jade installed on your machine.

Then create a file (call it example.js) using any editor you wish, and paste the code below.

example.js

var express = require('express')
var jade = require('jade')
app = express.createServer();
app.get('/',function(req,res){
var local_var = "I am a local var";
jade.renderFile('index.jade', {locals:{local_var:local_var}} ,function(err,html){
res.send(html);
});
});
app.listen();
console.log("Express server is started on port %s", app.address().port);

The first two lines imports the two packages that we are going to use and gives them each a name space.

app = express.createServer();
This allows us to have a handle on a concrete server

app.get('/',function(req,res){ .....});
defines a call back on our server. Every time the url path ‘/’ of our server is queried,
the function(req,res){….} will be called.

jade.renderFile('index.jade', {locals:{local_var:local_var}} ,function(err,html){
res.send(html);
});

renders the file ‘index.jade’ and pass the local variables defined in the hash {locals:{local_var:local_var}} to the template for proper variable substitution, in our case local_var (defined as “I am a local var”).
res.send(html) then sends the html that was rendered back to the client.

Our template file will look like the below. Paste it into a file in the same directory as example.js, and name this file index.jade.

index.jade
p= local_var
The above basically says that the paragraph will contain the value of what ever is defined in the variable ‘local_var’.

runnning the app

$ node example.js
Express server is started on port <port>

testing if it works
curl localhost:<port>

the above should give you

<p>I am a local var</p>

 

1 Comment

CASCON 2010

CASCON 2010 has been a fruitful one, it was there which I had my abstract published at ACM, I chaired for a conference for the first time. I had prepare weeks on my topic – which was clearly not enough and I was still as nervous as hell during the presentation.  My presentation was on cache-oblivious algorithms. “A cache-oblivious algorithm (or cache-transcendent algorithm) is an algorithm designed to use the CPU cache without having the size of the cache (or the length of the cache lines, etcetera) as an explicit parameter.”  as described by wikipedia. To present a topic, I had to understood the cache-oblivious algos completely, and it was not easy at all, since cache-oblivious algorithms are fairly new concepts. I was learning most of the material off this paper and 2 lectures videos from MIT, some of the algorithms were trivial, while others tooks days to understand the basic. The evaluation of my presentation not all positive, some people thought it was too deep into the algorithms. I hope to learn from this experience and improve my presentation skill for future workshops; for whatever conference it might be.

Leave a comment

Using finger with awk, finding out lab account info of your teammates in case you forgot

On one evening during my third year at U of T, I was working on an CSC343(intro to databases) assignment with my partner. When the assignment was finally completed I was overjoyed, with the assignment due in less than an hour, my partner put me in charge of submitting the assignment as he had to leave immediately and run some errands. When I was ready to submit the assignment electronically on a online app, the app asked me for my partner’s lab account name. I was in total panick, because I forgot his account name! I took a DEEP breath and tried to call my partner on his cell phone, but he didn’t pick up. “FML, my partner’s gonna kille me” I thought . I then messaged a friend and asked for consultation and possibly prayers. Instead of prayers however, she gave me a bash shell script that uses for loops, finger, /etc/pwd and grep. The script was magix, because it located my partner’s account info, and to this day, I still think she is l33t.

Now a year later, I wondered if there were a simpler way to find out the account information of a classmate by name (or part of his/her name) without using shell scripts. And here is what I came up with

export $REGEX=<name>
cat /etc/passwd | awk -F : '{print $1}' |xargs finger -l|awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

The command might look daunting at first, but if you break it down, it is really simple.
cat /etc/passwd displays various information of all the user accounts on system, it usually looks like this

jack:x:1000:1000:jack,,,:/home/jack:/bin/bash
riak:x:114:123:Riak Data Store,,,:/var/lib/riak:/bin/bash
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false

for more information on /etc/passwod visit here

awk -F : {print $1}

The above command basically says that for each line, print the first ($1) element delimited by the symbol “:”. In our case it will display all of the user account name.

jack
riak
mysql

The above would be an example of user accounts.

xargs finger -l

finger allows us to find more information on a given account name, including the user’s name. This is pretty essential.

awk '/Name: [a-zA-Z]*'$REGEX'[a-zA-Z]*/{print $0;getline;print}'

prints the line that contain the values that are defined by $REGEX.

So this little trick command allows the user to find account information based on the user’s registered user name, I hope this is useful to you, and I hope my WTF moment doesn’t happen to you.

,

Leave a comment