Node – install and test

One of my friends asked me what is Node and also how do you install and test to make sure it is working.

Well, Node is in essence a javascript server side environment that allows you to write code that will allow the event model to handle requests very quickly since it does not handle the I/O (well almost of all of the functions within node do not!) so there is no-blocking of code.

Node has a very quick and capable http class that allows for the user to create a server that is capable of doing some very quick responses to multiple connections.

To install Node you just need to do this in ubuntu

sudo -s
apt-get install python-software-properties
add-apt-repository ppa:chris-lea/node.js
apt-get update
apt-get install nodejs
exit

the first line allows you to be the root and thus the next commands are as the user root.

Or if you are using windows just download the executable from here

To test the node to make sure it is working with a basic “Hello world!” example if you save this below as helloworld.js.

var http = require("http");
// create the http server function req'uest, res'ult
http.createServer(function (req, res) 
{
    // 200 = means http response code of found page, then a array of the content type
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.end('Hello world!');  // write out the text
}).listen(8080,'127.0.0.1');
// listen on port 8080 on the ip address 127.0.0.1
console.log('We have started, check your browser on http://127.0.0.1:8080/');

I am using a higher number than 1024 for the port because below that you need to be a root user or administrator on windows to allow anything to connect to those port numbers.

So if you now run the program with the javascript file above as the second parameter

node helloworld.js

and then if you open your browser at 127.0.0.1:8080 then you will see

Hello world!

X session cannot start /tmp

If you get a error like

Error: X session cannot start /tmp 
Error: unable to write to the /tmp

or similar errors to the above when trying to log into KDE

it could be because you have run out of inodes (the disk structure that allows you to create new files) or hard drive space.

I had this same problem, and after doing

apt-get clean

to clean out of the temporary files that apt-get has used

and also since I am compiling up my own kernels, I deleted some of the

/usr/src 
 
linux-source
linux-source-2.6.32

directories and then I was able to start up X, which is great.. something of a better error would have been better.. cannot create file, make sure that you have the space!!. sort of thing..

also sometimes if your X session does not start, if you delete your home directory (The user’s home directory that you are trying to login to) .Xauthority

e.g.

rm ~/.Xauthority

because that is sometimes a lock on the xsession.

Rewrite Engine

Because I own the codingfriends.co.uk, but would like to point that to the www.codingfriends.com

Here is a good way to make it so that any requests to the domain name codingfriends.co.uk (rewrite condition of the host record) will redirect to www.codingfriends.com with a redirection of HTTP 301 (moved permanently).

RewriteEngine On
RewriteCond ${HTTP_HOST} !^(www)\.codingfriends\.co\.uk 
RewriteRule (.*) http://www.codingfriends.com/$1 [L,R=301]

Save that to the .htaccess root directory file. Hope that helps.

upstart – emit – start on network

I have been reading some of the funky things in ubuntu implementations that has been happening in the distribution. One of the things that I like is the upstart, this changes the way that init (the process of the computer system from boot to console/GUI for creating the different runlevels ).

In essence, it replaces the /sys/init daemon which handles the start up and shutting down, looking after tasks/services during boot/running and shutdown.

One of the things that is great is that the upstart will “emit” and signal to other services to “start on” when that signal has been emitted, to demonstrate this when a network card is activated for the Ethernet (for example) in the “/etc/network/if-up.d/upstart”

initctl emit -n net-device-up \

which basically says when a network device because in a state of up emit a signal, the state of being up is when the device is active. Then in the /etc/init/mountall-net.conf file

description     "Mount network filesystems"
 
start on net-device-up

is listening for the emitted signal of net-device-up and thus the upstart will start the networking filesystems.

Another example is within the /etc/init/mountall.conf there is the emitting of a signal

mountall.conf:emits virtual-filesystems

the signal is virtual-filesystems which is then picked up with the udev.conf with the

start on virtual-filesystems

since udev is a virtual filesystem in the /dev and /sys directories.

I find the stuff that Canonical is doing with the ubuntu linux distribution is just great.

iPhone – Linux – iTunes it works

I have been using gtkPod and also amarok, but sometimes I just missed that iTunes interface to sync with my iPhone 3GS (16GB).

Well, with using the virtual box ( you will need to get the Sun version and not the OSE (open source edition) because the Sun edition has the USB parts)

Then I booted up the virtualbox with a Windows OS, and installed the iTunes I was able to sort out my iPhone in the apple way!!.

It is still kinder annoying that apple are not making iTunes for Linux, but with this setup at least I am able to still use the iTunes interface with my phone and sync correctly.

Mono – web development on Linux

It is really easy to get some web c# (csharp) (asp.net) development within a Linux setup, since I use kubuntu which is derived from ubuntu. All you need to do is to

this installs the mono module for the apache2

aptitude install libapache2-mod-mono

to enable the module to work within apache2 you have to update the mods-enabled directory and to do this, just use the

a2enmod mod_mono_auto

which stands for apache2 (a2) enable (en) mod (module) and then the module name, you will need to restart apache2 for it to work

/etc/init.d/apache2 restart

that is it., then if you look within the /etc/apache2/mods-enabled you will notice the

mod_mono_auto.conf
mod_mono_auto.load

of course you could symlink them yourself, but it is just easier to use the a2enmod script.

Within the .conf file it tells apache what extensions to “listen” to direct to the module mono instead of either php module or just a static html web page.

To test the setup, just create a directory within your hosting directory this is normally /var/www/ or just place a file in that base directory /var/www it is up to you, but within that file you could create a basic test file like.

<%@ Page Language="C#" %>
<html>
  <head>
    <title>Apache2 Mod mono test page</title>
  </head>
  <body>
        <form id="form1" runat="server">
          <asp:label id="lbl1" runat="server">Apache2 Mod mono test page</asp:label>
        </form>
  </body>
</html>

the asp:label will be the main test, since that part of the csharp library as such. you should see something like

Apache2 Mod mono test page