Introduction to Node.js

In the last few articles, we’ve mostly discussed topics relating to Java and JavaScript. This article is no different, except this time, we’ll be talking about Node.js. If you recall the Meteor article, I mentioned Node.js briefly a few times in it – and this time, we’ll go into much more depth about what Node.js is and how you can use it.

To understand Node.js, we have to return to the original definition of JavaScript – which is, again, distinct from Java in that it’s a programming language that’s used to create web content. However, the standard packaging of JavaScript only allows front-end development. This means that web developers can use it to create the side of the website that users see and interact with.

Node.js is useful, however, because it extends JavaScript’s functionality to work as a back-end developer as well. Because Node.js is so focused on the server side of development, that’s going to be a lot of what we’re talking about in this article. We’ll go into detail about some current trends in Node.js, the benefits it can have to your development process, a few common mistakes to avoid when using Node.js, and some recommended frameworks that use Node.js for development purposes.

Node.js trends
The use of Node.js has grown a lot in the last couple of years since its introduction. In particular, a few of big companies, like WalMart and PayPal have adopted its use for their home servers. Every year, more and more people start using Node.js because of the many benefits of using it. The graph below shows the growth in just Google search queries for Node.js. This constantly increasing interest is a big reason that it’s an exciting and interesting time to use Node.js. As more people use Node.js, it’ll get both easier to use it

users.

Benefits of Node.js
There are quite a few reasons to use Node.js that we’ve gone over already, but here are some more. First, it’s fast. Because it’s based on JavaScript, it already has an edge in speed. However, there’s another reason that it’s so fast, based on a feature of Node.js called the event loop, which we’ll talk about more lately.

Speaking of JavaScript, it’s also easy to use Node.js because it’s based on JavaScript, which makes it a lot easier to get into and start using for a lot of people. Another reason that it’s beneficial is that it can handle a lot of concurrent tasks at once, which makes it easy to work with in real time. Additionally, it has special protocols written for its websockets, which make streaming data over its net framework very simple and quick.

Going back to the point I made earlier about it being adopted by a lot of people, the huge community surrounding Node.js is beneficial for the purposes of tech support and so forth. This also means that there’s an extensive – but uniform – codebase that anyone using Node.js can access to their own benefit.

Common mistakes to avoid
Although Node.js is a very easy to use framework, that doesn’t mean that there aren’t pitfalls you might experience while using it. This section covers a few common mistakes that people make when setting up and using Node.js.

The first is blocking the event loop. I mentioned the event loop earlier – it’s a single thread of data that can run necessary operations asynchronously, which means that they can occur with much greater speed. However, you can end up blocking the event loop by trying to parse a large json payload or a large output in one piece, or attempting a syntax highlight on a particularly large file originating from the back end of the server.

A second common mistake is to execute a callback twice. Redundancy is usually the name of the game, but trying to perform a callback more than once interferes with the single thread of data that usually runs through the program.

Another common mistake is to refrain from logging things, or to log progress poorly. Documentation is something that’s often not created as stringently as it should be. In situations where work is poorly documented, it makes it hard to see a development process retrospectively, which is very important in case there’s a problem with a project a while after you’ve stopped working on it.

Along a similar vein, not testing code – or testing it inadequately – is another kind of situation where laziness can become a big problem later on. One important thing to remember when testing code, is to make sure that you test it in a set of circumstances that are as close to the typical user environment that it’ll be in when it’s actually distributed. Something might work fine in a developer environment and then as soon as it’s distributed, it could end up functioning poorly in a user environment.

Finally, the last mistake we’ll talk about here is to not use static analysis tools. Static analysis tools help you debug code that you’re writing while you’re still writing it. This can help you avoid making writing mistakes like writing out conflicting expressions and making sure that you uphold best practices such as only declaring variables once and so forth.

Recommended frameworks
There are a plethora of frameworks that use Node.js to pick from, these days, as a result of its sweeping popularity. Although you can’t really go wrong with any of them, the choice can be a little overwhelming. For that reason, the final portion of this article is to go over some of the best frameworks out there to at least give you an idea of where to start if you’re thinking about getting into using Node.js.

The first one of these is Mojito, which was developed by Yahoo. It’s specifically used for mobile development and it supports HTML 5 in addition to Node.js, which it uses to serve as a development tool for both client and server sides.

io is another framework which is based on bidirectional event-based communication. Its claim to fame, as it were, is that it works on every platform, browser, and device, and that it features collaborative documentation from its users and developers.

Another framework is Meteor, which is something that I’ve already covered in detail in a previous article. In general, however, Meteor is a framework that supports both mobile and non-mobile development, and focuses on simple but powerful code for complex uses.

js is another framework, this one focused on development for business. It uses a familiar MVC pattern which is a lot like Ruby, a language that a lot of people are familiar with.

The final framework we’ll talk about here is Total.js. As the name suggests, Total.js bills itself as a complete solution for any kind of framework needs and supports development for things from full-featured business apps to simple to-do lists.

This article is really an introduction to the many facets of Node.js, but it has hopefully given you an idea of what Node.js is and how you can get started using it – as well as why you might want to.