If you’ve got experience of using Javascript I can’t recommend trying your hand at node.js more. It’s really versatile and easy to pick up, with it’s modern architecture it performs really well on processing a lot of information through things like feeds and can integrate well with a website built with a LAMP setup.

For that reason it’s perfect for JSON feeds, especially for a mobile app or website which has a lot of traffic.

Here’s a basic example of how you can pull information from a MySQL database and print it to the browser.

var http = require('http');

var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'database user name',
password : 'database password',
database : 'database name'

http.createServer(function (request, response)
console.log('Creating the http server');
connection.query('SELECT * FROM table WHERE field like "%search for%" limit 0,10', function(err, rows, fields)
console.log('Connection errors = '+err);
console.log('number of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});

Node.js doesn’t need apache to run but if you’re running a node.js script on the same server along side the rest of your website we can set this up with some basic apache config.

Node.js will probably be running on it’s own port so make note of what that is.

Take a look at your apache http.d config file to make sure the following two lines aren’t commented out. These two lines will enable two apache modules “proxy” and “proxy_http”

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

The next step is to edit the virual host file and add the ProxyPass rule.

DocumentRoot “/var/www/example.com”
ServerName example.com
ProxyPass /node http://example.com:8080

This will make example.com/node load up the nodejs script running on port 8080.

By setting up a node script like this we can run a nodejs script along side a PHP website on the same server. Perfect for small projects.

Node.js is an amazing piece of technology for helping you develop scale-able mobile friendly websites and apps. It’s growing in popularity massively due to it’s logical and modern approach to web programming. Where as before you would need to learn something like Javascript for front end development and PHP for back end, Node lets you do everything using relatively the same code as it using a back-end language very similar to Javascript. This has made it popular with new developers wanting to learn full stack development and experienced developers who already know Javascript and similar languages.

On an Ubuntu server you can install the Distro stable version of Node.js via the terminal by running

sudo apt-get update
sudo apt-get install nodejs

Once that’s complete you will want to install the Node.js Package Manager (NPM)

sudo apt-get install npm

Once that’s all installed we can test the installation.

Create a file anywhere on your server called hello.js

in the file write the following:

console.log(‘Hello World’);

Then back on the terminal run

nodejs hello.js

The terminal should echo out “Hello World” note we’re typing “nodejs” and not just “node”. It’s called nodejs because node conflicts with an old Ubuntu package.

At this point you will probably be keen to see just how much of your Javascript knowledge will come in handy when working with node.js. It really is a strange feeling writing back end code in a front end language but it’s a modern take on web development.