UPDATE: New packages and further items
What a difference a year makes. Last year at the first conference solely dedicated to JavaScript, JSConf 2009, there was a big focus on the uses of JavaScript on the client side. This year, there was a marked change with server-side JavaScript taking up about half the sessions at JSConf 2010 this past April. The rise of server-side JavaScript has been in large part to two platforms, Narwahl and node.js. Node in particular has garnered a bit of attention lately, especially with the node knockout which happened very recently. The winners in the node knockout competition alone should give you some clue as to its potential.
Why Node? Simply put, the purpose of node.js is “To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.” This stands in contrast to many other concurrency models where threads and synchronization blocks are used, which can cause any number of issues. Because of its lock free nature, and a bigger freedom from deadlocks, it allows for a more approachable way to concurrency. The node.js site itself has a great amount of resources here to get started.
In this series, I’m going to focus a little bit on getting started on node.js and how you can mix the Reactive Extensions for JavaScript into your node solutions.
Getting Started
One downside has been in the past for me, being a developer who uses Windows frequently (given that I’m employed by Microsoft), has been the lack of native support for Windows. Fortunately, with the release of node.js version 0.2, there is now support for Windows, albeit through Cygwin. Following the instructions here, I was able to get it to install on any number of machines, from XP, to Windows 7 to Windows Server 2008 R2.
When installing Cygwin, I chose the following packages:
- Dev (install all works just fine for all of them if space is not an issue, and I’d recommend that)
- gcc g++ C++ compiler
- gcc mingw-g++
- gcc4-4++ G++ subpackage
- git
- make
- openssl
- pkg-config
- zlib-devel
- Python – install
- Web
- wget
Once that installs, before you’ll be able to successfully build anything, you should do a rebaseall by doing the following steps:
- Close any instances of Cygwin
- Launch a command prompt
- Execute ash.exe (commonly found in C:\Cygwin\bin)
- Execute ./rebaseall –v
You should see results similar to the following:
/usr/lib/abc.dll new base = 1234, new size = 1234
Now it’s time for us to pull down a fresh copy of node.js. You can either do it from wget and unpack it as the following:
$ wget http://nodejs.org/dist/node-v0.2.0.tar.gz $ tar xvf node-v0.2.0.tar.gz
Or using Git, we can pull that down by the following command, if you installed Git of course like I told you to:
$ git clone http://github.com/ry/node.git $ cd node $ git fetch --all $ git tag $ git checkout v0.2.0
If you’re using a proxy server, like I was in several installs, you can get around that by setting the http_proxy environment variable:
$ export http_proxy=http://someproxy:1234/
Then you can start to configure and build such as the following:
$ cd node $ ./configure $ make $ make install
Let this chug for a little bit and it should build just fine. You can check for success by displaying the version of node such as:
$ node --version v0.2.0
Another thing to notice is that we need to configure the DNS. Although Cygwin itself relies upon the Windows DNS for its queries, node.js uses the c-ares library that relies on /etc/resolv.conf
file. Edit this file (if found, else create it) and add the entries to the Google public DNS entries:
nameserver 8.8.8.8 nameserver 8.8.4.4
Now, it’s time to set up our first file, called example.js and put it in the root of your node install, just for simplicity sake at this point. We’ll create the typical web server example where we write our header and a body containing Hello World from Cygwin.
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html'}); response.end('<b>Hello World from Cygwin</b>'); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/');
And then we can start the server by the following:
$ node example.js Server running at http://127.0.0.1:8124/
And now we can see the fruits of our labor by going to the address in our browser:
And there you have it, ready to roll with node.js on Windows (via Cygwin).
Conclusion
Here we’ve just scratched the surface of what we can do with node, now that it can be accessible to the many Windows developers out there. Node, with its engaging community, is giving developers options for creating interesting scalable applications using JavaScript (yes, JavaScript). In this series, I hope to dive a little bit into it as well as how the Reactive Extensions for JavaScript can play a part.