Ubuntu and Node.js for the Uninitiated .NET Dev

It’s been a about 12 years since I’ve used Linux as a desktop. My last foray was spending a marathon two hours with a friend, who ironically now works for Microsoft, getting 3D Acceleration to work. Double Ironically, Microsoft is now introducing full bash support for Windows 10. The fruits of our labor ended with me playing Tux Racer for about 30 minutes, losing intrest, and moving back to good old Windows 2000.

Prior to that, I had loads of experience installing the LAMP stack on web servers. I even worked for a local ISP setting up their ‘professional tier’ of web hosting. Later, a zealous admin ran a “rm -rf” on the home directory instead of the specific user’s directory they were removing. I had the pleasure of informing all of the customers that, “Backups are your responsibility”. The lesson learned is that with great power comes great responsibility, and Those who want confirmation prompts need not apply.

Web api and SQL Server are currently my drug of choice for web development. However, it’s hard to ignore the ‘buzz’ that is Node.js (especially when you work with nodevelopers). I’ve decided it’s best to dive in head first. I decided I was going to go all in and use Linux for the added bonus of local Docker containers.

How hard is it to get an environment up and going? When I started with PHP I would have to download the Apache and the PHP source and compile them. Luckily, things have changed a lot in the past ten years and getting Node.js up and running is incredibly easy. All of the necessary programs can be installed via a command line tool called ‘apt-get’. Apt-get is the Debian package manager. It’s basically ‘nuget’ for programs. The closest you can get on Windows is a utility called ‘Chocolatey’. I wrote an article with my preferred install script a while back, fair warning I haven’t tested it in a while.

Installing Ubuntu

Disclaimer: It will take you more than 10 minutes to install Ubuntu, once thats done, getting node running is a quick process. I chose to install a Linux VM on Parallels. Virtual Box is just as good and Free.

Setting up the Node.js development environment

The first thing I did was install git. This isn’t necessary at all to get Node.js running, but git is the de-facto source control solution, so we might as well install it. ‘sudo’ allows a command to run with elevated privileges, you’ll be asked for your password to confirm.


$ sudo apt-get install git

Install Node.js


$ sudo apt-get install nodejs. This is the Javascript runtime. 

Install node package manager. We don’t need this for a hello world program, but we will once we want to start installing packages. NPM is used for downloading Node.js utilities and backend code.


$ sudo apt-get install npm

Now install ‘bower’ using the npm utility. Bower is another package manager typically used for front end Javascript libraries


$ sudo npm install -g bower

Make sure we are in the ‘home’ directory and create a source directory to work in.


$ cd ~
$ mkdir source
$ cd source

Here is where I spent most of my time. I tried to use ‘pico’ as my text editor, but could not figure out how to paste from an outside program. I ended up using ‘gedit’ and pasting in the following code from https://howtonode.org/hello-node I do have the longterm goal of learning ‘vim’, apparently it is the holy grail of text editor efficiency.


$ gedit hello-world.js

Hello world javascript code from howtonode.org.


// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Now we run the node command, and we have a basic web server


$ nodejs hello-world.js

Open Firefox, and browser to http://localhost:7000

Node.js Hellow World

Thats great and all but not entirely useful. Suppose I want to server up some files. Any easy way to do this is to use the npm packages ‘connect’ and ‘serve-static’.


$ npm install connect serve-static

Create a file with the sample server code.


gedit static-server.js

Enter the following code:


var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8080, function(){
    console.log('Server running on 8080...');
});

Drop in a sample html file and run the following command. You should be able to access it at http://localhost:8080/yourfile.html.


$ nodejs static-server.js

Static File Server

Things have come a long way since the days of having to compile your web server. I’ll be interested to find out of Node.js lives up to all the recent hype. So far the Kool-aid goes down smooth.

Leave a Reply

Your email address will not be published. Required fields are marked *