27 :: Knowing node.js

Some months ago, I heard a couple of my more tech savvy colleagues espouse the virtues of node.js.  I asked them what it was and all I can remember about the answer was it had the word websocket in it. So I went to the node.js website to learn more and read the elevator pitch:

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

So that didn’t help me much for me. And so node.js remained a mystery to me until recently when I was able to ask a developer at the Allied Media Conference DiscoTech lab why he and others use node.js.

He said that, as a web developer who works in JavaScript, node.js allows him to plug into a JavaScript backend of a noSQL database made also from JSON and JavaScript, either in the form of CouchDB orMongoDB.  So it’s not surprising he approved of my concentration on JavaScript mastery.

So, that made more sense to me.  Node.js helps those used to using JavaScript on the front-end of a web application to also use JavaScript on the back-end.

But I didn’t realize until tonight that what this meant is that you can use use node.js to run the web server that runs the front end and the backend databases.

Let’s start with the server module. Create the file server.js in the root directory of your project, and fill it with the following code:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");

That’s it! You just wrote a working HTTP server. Let’s prove it by running and testing it. First, execute your script with Node.js:

node server.js

Now, open your browser and point it at http://localhost:8888/. This should display a web page that says “Hello World”.

The above is an excerpt from the free ebook version of The Node Beginner Book by Manuel Kiessling which he describes as a ‘novice to advanced novice’ book. And it is and I really appreciate it because as a novice trying to make to ‘advanced novice’, I will tell you that the space between beginner and expect is an oddly desolate place.

Works like Kiessling’s are very useful because they make the connection why a particular concept (in this case, the anonymous functions of JavaScript) comes in so handy when doing useful (like asynchronous database fetches). I’d been previously introduced to the idea of anonymous v.s. named functions before but none of the tutorials really ever bothered to explain why the differences made any difference.

Yes, I am one of those people that need to be told why something is worth learning or I won’t even try to remember it.

I’m also finding that I appreciate opinionated style guides when it comes to coding, like this one for node.  There are some guidebooks on JavaScript patterns that I’m hoping to check out soon.

Now, I haven’t gone about actually downloading and working with node.js on my (ubuntu) laptop yet or on a AWS SDK since I really don’t have a use-case that I want to work towards yet. According to this page – Convincing the boss – using node.js for a single page AJAX heavy app is a good reason to use it – among other possibilities that are less likely for me to take on – along with some strong words of warning not to use the technology just for nerd-cred alone.

Which is fair enough. But if I have enough node.js understanding down just to know that anonymous functions in JavaScript are worth the time to appreciate, well that just might be enough to be useful to me.

Leave a Reply

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