One of the most talked about technologies on the web right now is node.js, which is a server side implementation of JavaScript. That’s not really anything new, but what is new is how it’s built. The framework is built on top of Google’s v8 engine, which is lightning fast. It’s made to create “scalable network programs” by making nearly everything work asynchronously. I don’t want to bore you with all the little details, but basically it’s really fast and doesn’t use very much memory. If you feel like you need the nitty gritty details, check out the node.js About Page.
Today, we are going to build a simple application that is going to pull the rss feed from this site and display the raw xml in the browser. The first thing we need to do is get node.js installed, for me this was on Mac OSX. With that said, you need to download the latest version from the node.js Download Page. Once you have it downloaded, expand the contents to a directory of your choice. Now, open up a terminal and change the directory to where you put the node.js contents. Then, run the following commands:
make
make install
Once everything is installed we are ready to try out some code. The first thing we are going to create is a simple script to make sure everything is working as planned. Below you’ll see our starting script.
http.createServer(function(req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end("Switch On The Code Rocks My Socks Off!");
}).listen(5073, "127.0.0.1");
Now, save the code wherever you would like and go back to your terminal and change the directory to wherever you saved the script. In order to actually run the code you type in node name_of_script.js
– replacing “name_of_script.js” with the name of your script. To check out the results open a browser and go to 127.0.0.1:5073
and you should see “Switch On The Code Rocks My Socks Off”. In my case I named my file “test1a.js”, therefore my command was:
Okay, let’s walk through the first application. The very first thing we do is include a library. In node.js we do this using the require
function, passing in the library to include. The http library allows you create and handle typical server functions. The next line of code creates an HTTP web server. A typical web server takes an anonymous function that will handle each request. Passed into the function are two parameters, the request itself and the response; the response is used to send a response to the client. At the end of the server declaration we call listen
on the created server, passing in the port and host to listen on. Finally, inside our request handler we do two things. We first write the content type into the response header. Then, we respond to the client with the text “Switch On The Code Rocks My Socks Off!” ending our response.
Let’s move on to something a bit more complicated. The next application we are going to build will serve up a couple of static files. These files include an index HTML file and a CSS file. Basically we are serving up a webpage, not a very complicated one but the ideas used below can be expanded into a full fledged static file server. The following code sets up the server just like previously, except we are adding another standard library called fs. The new library, fs, takes care of file handling – reading and writing.
fs = require(‘fs’);
http.createServer(function(req, res) {
}).listen(5073, "127.0.0.1");
Next, we need to add code for handling the request, just like before this goes into our anonymous function. Below you find the rest of the code, which is explained after.
fs = require(‘fs’);
http.createServer(function(req, res) {
if(req.url == "/" || req.url == "/index.html") {
res.writeHead(200, {‘Content-Type’: ‘text/html’});
res.end(fs.readFileSync(‘index.html’));
} else if(req.url == "/part1.css") {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(fs.readFileSync(‘part1.css’));
} else {
res.writeHead(404, {‘Content-Type’: ‘text/plain’});
res.end("Page Could Not Be Found");
}
}).listen(5073, "127.0.0.1");
The handler function checks the incoming url and if it is either the root or “index.html” then the text in the HTML file is returned. If the url is for the CSS file then the contents of that is returned. For the most part it looks like the first program we created, but there are some differences. For one we return a different content type for the HTML file – which makes sense. Also, to write out the contents of a file we have to use the function readFileSync
. That function reads the file synchronously and returns the contents, which is then written to the response.
That pretty much takes care of our intro into Node.js. Make sure to check back for more Node.js tutorials in the future. If you have any questions feel free to leave a comment below or check out the forums.