Fun With Node

| Comments

Now I’m starting to have fun with Nodejs. This is because the excellent tutorials from Node tuts and because WebStorm is awesome.

Here is a first line of javascript:

hello.js
1
console.log('behold, the pizza is in the oven.');

Let’s run it with Node:

node
1
2
$ node hello.js
behold, the pizza is in the oven.

Why is this even cool? We just ran javascript without a browser, on a server.

Blocking vs non-blocking

This is an example of blocking code:

read_file.js
1
2
3
4
var fs = require('fs');
var contents = fs.readFileSync('/etc/hosts');
console.log(contents);
console.log('Now on to the next function');

This will run line by line, and all other processes are blocked until its finished.

With Node, we should be writing non blocking code, like this:

read_file.js
1
2
3
4
5
6
var fs = require('fs');
fs.readFileSync('/etc/hosts', function(error,contents) {
console.log(contents);
}

console.log('Now on to the next function');

The readFileSync accepts two parameters, the file to read and another function: the callback function. The callback function has 2 parameters, one that contains the error (if there is one) and one that contains the result.

We can also write the function like this:

read_file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var fs = require('fs');

//a callback function returns the result, asynchronuously.
//a callback function always has 2 parameters, first is the error and the second is the result
function callback(err, results) {
    if (err) return handleError(err);
    console.log(results);
}

//in node, in third party libraries, the last parameter from a function is the callback.
fs.readFile('/etc/host', 'utf-8', callback);

//we can read a second file. This would run in parallel, it's non-blocking!
fs.readFile('/etc/vim/vimrc', 'utf-8', callback);

I think it’s more readable to add the callback function as a parameter and then we can omit ‘callback’ and use an anonymous function:

read_file.js
1
2
3
4
5
6
7
//the callback function is the last argument
//the callback function always has 2 arguments, error and results.
fs.readFile('/etc/hostst', 'utf-8', function (err, results) {
        if (err) return handleError(err);
        console.log(results);
    }
);

So this is the essence of Nodejs:

  • stop writing threads and other procedural blocking code
  • start writing asynchronous code
  • in a third party lib the last parameter is the callback
  • in a callback function the first parameter is the error, the second parameter is the result.

Installing Node and Webstorm on Debian

| Comments

This is how to install Node on a Linux system:

Install Node
1
2
3
4
5
6
7
8
9
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://npmjs.org/install.sh | sh

For Webstorm we need the SunJDK.

Install SunJDK
1
2
3
4
5
#get the JDK
$ wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com" "http://download.oracle.com/otn-pub/java/jdk/7u45-b18/jdk-7u45-linux-i586.tar.gz"

#unpack it
$ sudo tar zxvf jdk-7u45-linux-i586.tar.gz -C /usr/lib/jvm/

Check the current JVM (openJDK):

check JVM
1
2
3
4
5
6
update-alternatives --display java
java - auto mode
  link currently points to /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java
/usr/lib/jvm/java-6-openjdk-i386/jre/bin/java - priority 1061
  slave java.1.gz: /usr/lib/jvm/java-6-openjdk-i386/jre/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-6-openjdk-i386/jre/bin/java'.

Check the -priority value. Mine is 1061 as you can see.. This means we need to install the SunJDK with a higher priority, i.c. 1062.

Run all these commands (make sure the path is right though).

JVM
1
2
3
4
5
6
7
8
9
10
11
# set the priority to 1062!
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_45/bin/java 1062
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_45/bin/javac 1062
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_45/bin/javaws 1062
$ sudo update-alternatives --install /usr/lib/mozilla/plugins/libnpjp2.so libnpjp2.so /usr/lib/jvm/jdk1.7.0_45/jre/lib/i386/libnpjp2.so 1062

#check the java version
/usr/lib/jvm$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode)

Install WebStorm

Webstorm
1
wget http://download.jetbrains.com/webstorm/WebStorm-7.0.2.tar.gz

Basic Auth

| Comments

Ik ben al een tijdje bezig met het bouwen van web applicaties met Angularjs en een ASP.NET Web API backend.

Authenticatie is met deze stack bijzonder complex. De documentatie vind ik niet erg duidelijk.

Als je deze howto volgt, dan zijn er een paar dingen niet duidelijk. Bijvoorbeeld, waar komt http://localhost:port/Token route vandaan?

Dit komt dus van OWIN.

Owin

De Web Api Visual Studio templates krijgen zo wel veel weg van een kerstboom.

Ik wacht nog even met token based en ga eens kijken of ik Basic Auth kan toepassen.

CrunchBang

| Comments

I have been using Linux forever. In 2003, I registered at Linux Counter.

this.

I have been using tons of Linux distro’s. I’ve started with Caldera, went on to RedHat and used Slackware for a while. After then I sorta stuck to Debian and Ubuntu. And recently I’ve found CrunchBang.

I think it’s quite good.

crunchbang

Why CrunchBang is cool #!

  • lots of keyboard shortcuts
  • terminal here in the file explorer
  • easy software install fromt OpenBox menu
  • feels like home: #! is based on Debian Wheezy

Here’s a review. I did some minimal tweaks, let’s describe them below.

Conky

To use Conky to display keyboard shortcuts is brilliant for those with memory leaks. I’ve added some exta shortcuts for VIM, NERDTree and Terminator. Here is a link to my conkyrc.

crunch-empty

Terminal emulator

The default terminal is Terminator. Here are the most basic keybindings, copied from the CrunchBang site:

  • Ctrl-Shift-o: Split the screen horizontally
  • Ctrl-Shift-e: Split the screen vertically
  • Ctrl-Shift-t: Open a new tab
  • Ctrl-Shift-w: Close the current terminal
  • Ctrl-Shift-q: Quit Terminator

Navigation:

  • Ctrl-Shift-n / Ctrl-Tab: Next terminal within tab
  • Ctrl-Shift-p / Ctrl-Shift-Tab: Previous terminal within tab
  • Ctrl-Pageup: Move to next tab
  • Ctrl-Pagedown: Move to previous tab
  • Shift-Pageup: Scroll back through the current terminal
  • Shift-Pagedown: Scroll forward through the current terminal

I decided to change the colorscheme to Solarized from Ethan Schoonover.

Here is the github repo with the config file for Terminator.

Vim

1
$sudo vim /etc/vim/vimrc

And uncomment ‘syntax on’.

Next install Pathogen:

1
2
3
4
5
##copy and paste this in the terminal:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
    https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim

Now install Solarized for Vim:

1
2
$ cd ~/.vim/bundle
$ git clone git://github.com/altercation/vim-colors-solarized.git

And install NERDTree

1
2
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git

Now go ahead and creat a .vimrc:

1
$vim ~/.vimrc

Copy this in the .vimrc. This will set a statusline and line numbers. It activates pathogen and sets the colorscheme:

vimrc
1
2
3
4
5
6
7
set statusline=%<%F%h%m%r%h%w%y\ %{&ff}\ %{strftime(\"%c\",getftime(expand(\"%:p\")))}%=\ lin:%l\,%L\ col:%c%V\ pos:%o\ ascii:%b\ %P
set laststatus=2
set number
execute pathogen#infect()
syntax enable
set background=light
colorscheme solarized

And the result will be this:

vim

You can also choose the solarized-dark, but I think the light version is easier on the eye. Now you can go coding, be mighty productive and make lots of money. Or not.:)

NERDTree Cheatsheet

| Comments

Install VIM, Pathogen and NERDTree.

NERDTree
1
2
3
4
5
6
7
8
9
10
11
12
13
* o       #Open file or directory in the NERDTree Explorer
* t       #Open file in new vi tab
* i       #Open file in new horizontal split
* s       #Open file in new vertical split
* p       #Go to parent directory
* r       #Refresh current directory
* ctrl+ww #Switch between the NERDTree Explorer and File Buffer

* :NERDTree      #Open the NERDTree Explorer
* :NERDTreeClose #Close the NERDTree Explorer
* :tabp          #Go to previous vi tab
* :tabn          #Go to next vi tab
* :tabnew        #Open new vi tab

Ubuntu Gnome

| Comments

There is a version of Ubuntu that has Gnome 3 as desktop manager. I prepared a box for updating my Octopress blog.

Here is how.

complete transcript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#install RVM
sudo apt-get install curl
\curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements
rvm install 1.9.3
rvm use 1.9.3 --default

#install rubygems and rails
rvm rubygems current
gem install rails --no-rdoc --no-ri

#install git and heroku
sudo apt-get install git
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
#create keys:
ssh-keygen -t rsa
heroku keys:add
heroku login

#clone the Octopress site from Heroku (or a new one from the Octopress site)
git clone git@heroku.com:enig-savan-2778.git
gem install bundler
sudo apt-get install rbenv
rbenv rehash
bundle install


#install Sublime Text
sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get install sublime-text-2

#add your info to Git
git config --global user.name "Jacqueline"
git config --global user.email "jacqueline@linux02"

By the way, install 1.9.3 wit RVM doesn’t work with Debian Jessie (yet, however 2.0.0 works fine).If you are like me and you don’t do Rails development, you can install the Octopress requisites on Jessie like so:

complete transcript
1
2
sudo apt-get install ruby ruby-dev rbenv
sudo gem install bundler

So, in this case it is actually convenient that Debian never uses the latest version of sofware.

Peppermint OS

| Comments

Peppermint OS is a minimal Linux distribution based on Ubuntu. Its desktop manager is OpenBox. So why did I choose this distribution? Well, it is the only one that does not crash in Vmware Workstation 9 on Windows 8.

Peppermint logo

Add More Computers to Blog From With Octopress and Heroku

| Comments

Here is how to add more computers to blog from with Heroku and Octopress.

On computer 2 (assumes Rails and all dependencies are installed):

First clone your Octopress site. Then cd into the clone, generate, commit and push.

clone your blog from heroku
1
2
3
4
5
6
git clone git@heroku.com:lazy-turtle-2778.git
cd lazy-turtle-2778
rake generate
git add .
git commit -m 'testing from 2nd computer'
git push origin master

Now you can manage your Octopress blog from computer 2.

The next day you want to add a post from computer 1. On computer 1, check the name of the remote repository:

check name of remote repository
1
2
3
jacqueline@oude-laptop:~/Desktop/code/octopress$ git remote -v
heroku    git@heroku.com:lazy-turtle-2778.git (fetch)
heroku    git@heroku.com:lazy-turtle-2778.git (push)

Now you need to fetch the files from the heroku repo and merge them.

fetch and merge
1
2
jacqueline@oude-laptop:~/Desktop/code/octopress$ git fetch heroku
jacqueline@oude-laptop:~/Desktop/code/octopress$ git merge heroku/master

And all is set to manage your blog from computer 1.

Disclaimer Voor Nieuw Weblog

| Comments

Mijn weblog werd voorheen gehost door Posterous, maar deze dienst is opgehouden te bestaan. Helaas. Wat nu? De zoekmachine bracht mij bij Octopress.

Octopress logo

Octopress heet niet voor niets ‘a blogging framework for hackers’. Om te beginnen is het noodzakelijk dat je Ruby on Rails op je machine hebt draaien. Vervolgens kun de site met Git publishen naar Heroku, maar dit kan ook naar Dropbox of Github zelf.

Daarna is het een kwestie van.. editen van tekstbestanden, zoals ik nu deze post zit te kloppen in Sublime Text in Markdown.

Helaas is het alleen maar mogelijk te posten vanaf deze laptop. Dat is wel minder, even vanaf een andere werkplek een update schrijven zit er dus niet in.

Last but not least: ik heb de layout gebruikt van oct2. Design is namelijk niet mijn sterkste kant.

Kortom, zonder al deze resources zou je nu dit weblog niet aan het lezen zijn. Ik zeg dan ook: “Hulde aan ieder dit mogelijk heeft gemaakt en driewerf hoera voor Open Source.”

En dat allemaal op deze dag die ik wist dat zou komen.