Discovery of Node.js by a Java Developer

A journey to node

Hello everyone, first things first. My name is Devendra Tiwari, I am Java Developer from the beginning of time. I always praised about how good is Java, How we can use Java in android to make beautiful of apps, how it’s the first choice for enterprises and I am fond (mostly used to) of verbosity of Java. I have developed at least 15 projects on Java. And I am not afraid to say that Java brings food on my table.

Of course, I knew about other languages, I have done a few here and there tasks in Ruby and PHP. I had used Javascript in my college days, and I was quite obsessed with document.getElementById(). Back then, Java was “the server side” language and JavaScript was used to manipulate stupid DOM elements. I was never an UI person, so I chose Java for rest of my career.

I was doing great with my dear language for a year or two, and then there was this AJAX BOOM. Everybody was using it, for no reasons. Of course was none of my concerns. I don’t give a shit what you do on the browser, I will entertain only when request reach server, remember I am “the server guy”.

Then I came to know about some platform, which enables “so called developers“ (not the real ones, real ones use Java, remember) to use JavaScript on the server. I said how lame. Are they going to create beautiful pages on server :D ?

I was wrong. Node.js grew almost exponentially. Within a year or two it was “the new thing” in the cafeteria chats. People used to talk about its quick setup of the running server, fewer lines of codes. I defended with Spring Boot. They argued about non-blocking IO, I reminded them about netty. I really tried, but they had a point. I started missing my lunches.

A few nights back, my friend (who is also a Java developer)switched her job. In her new job, she had to use node.js. She asked for help. So here I am, trying to share a few questions I had in my mind while I was looking about node.js.

CAUTION: If you find any F-word, that’s a typo, I always meant Duck. :D

1. What is node.js after all?

Well the index page says:-

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Wait. Wait. Wait. What?

Ok, let me break it for you;

Node.js is simply a library written for V8. Now the bigger question what is V8? Well, In simplest words, V8 is an implementation of JavaScript written in C++. It lets you run standalone JavaScript applications (among other things). V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs. V8’s stop-the-world, generational, accurate garbage collector is one of the keys to V8’s performance.

If you are from Java World, you can relate it something to JVM, but on steroids.

So, Node js comes with features which I always dreamt about in Java. It’s event-driven, non-blocking IO is “the thing”. If you are not familiar with the terms, here is what they really are:-

Event-driven :- So, think of a scenario, say if you click the save button, an alert pops up. So, your ‘click’ is an event, which caused the alert button to pop up.
Everything in node.js is event driven. There is the main thread, which listens for ‘events’ and then triggers the corresponding callback function (in our case, showing the pop up).

Non-Blocking IO : — First, What is blocking IO? Consider a scenario, when a thread asks for some records from the database, it waits there until the database returns with some records. That’s blocking IO, you have simply blocked that thread until database returns something to it, your JVM can’t use it for that time being. And you must be knowing how costly (in terms of time complexity) IO operations are.

But not in this world. Bitch!

With node.js event-driven programming, you can give a callback function (you must be aware of it if you ever used JQuery), which will be eventually called when your database is done whatever it was doing. That’s Non-Blocking IO.
By now you must be aware that, its nothing scary as it seems, perhaps “interesting” seems a better world.

1.1 Why Node??

>Because it is popular
Simple right? Lots are people (and companies) are interested in node js, it has the fastest growing communities (better SO questions to copy and paste solutions) and largest library counts.

> Asynchronous IO
As told earlier, Async IO is one of the most desired features among other communities. Node is built to handle asynchronous I/O from the ground up and is a good match for a lot of common web and network-development problems. In addition to fast JavaScript execution, the real magic behind Node.js is called the Event Loop. To scale to large volumes of clients, all I/O intensive operations in Node.js are performed asynchronously.

Queries for some of the newer databases, like CouchDB, are written in JavaScript. Mixing Node.js and CouchDB requires no gear-shifting, let alone any need to remember syntax differences.

Why does it matter? Because Async IO improves the performance of a system considerably. How? Remember the word Google?

> Language

Node is all Javascript, so you don’t need different languages for front-end and back-end. So more dependency on “the server guy”. If you know the language and the flow, you can too fix the problem. That’s where a new term is coined, called “Full Stack Developer”, yeah, Node js is the culprit.

> Simplified Build processes
I love maven, I am a fan of it. Maven has revolutionized Java programming. But there is just one issue. I have to write specifications in XML, I mean seriously, XML is was not designed to support programming logic.
With Node js, you can do it all, knowing just one language.

When database spits out data, developers go to an extent converting those tuples into meaningful objects, remember all those mappings, tons of POJOs, Hibernate and what not. Well, don’t need to do anything with node, the result comes in JSON format.

Clearly, Json is the first class citizen in node js. Not quite correct when I talk about other languages, of course, there are mappers, but then are they are just mappers.

> Fast and Scalable
Who doesn’t loves speed? People love to praise the speed of Node.js. The data comes in and the answers come out like lightning. Node.js doesn’t mess around with setting up separate threads with all of the locking headaches.

There’s no overhead to slow down anything. You write simple code and Node.js takes the right step as quickly as possible.

There’s one thing we can all agree on: At high levels of concurrency (thousands of connections), your server needs to go to asynchronous non-blocking…you can’t go creating threads for every connection.

2. What took so long, to make Node js?

I mean, Javascript was here, right after Java in 1985. These were like twin sisters. Then what took years to enable it on servers, while Java was quite easily became King of the server.
Well, That’s the question I am still looking into.

3. Where does node js runs, like tomcat or what?

Well, node.js applications (aka node server aka node app) does not need tomcat. It runs directly on V8 platform. Though you will have to install node. Wanna know how? Try this new thing called GOOGLE.

4. What is NPM?

Well, think of npm (node package manager) as a CLI tool to get modules (or libraries in java world) created by others. Sounds similar to Maven (or gradle or ant). Yes it is, a little bit similar, but I think it is more similar to Maven Central (or JCenter)+ Maven, Maven Central where all the developers host there libraries. And maven is a CLI tool to get those dependencies.
In easiest way possible, It’s a way to reuse code from other developers, and also a way to share your code with them, and it makes it easy to manage the different versions of code.

NPM comes as a side product when you install node. So you need not worry about installing it, but yeah, one should definitely go through some of its commands, before diving deep into node js. (hint: node install is the one you’ll be using a lot)

5. MVC Frameworks

Unlike, Java MVC frameworks warfare, where Spring have almost won the battle. In node, the war is still on, though Express.js is having some serious lead, others like Hapi and Meteor.js are not far behind.
Express.js? Never heard of it?

Well, let’s just say it’s a simple framework which provides some good folder structure for starters, and provides some of the basic features out the box, like database adapters, ORMs, HTTP Request clients etc.

Basically, It’s good to have a framework because, without it, you might have to reinvent the wheel. Apart from that, others might find it difficult to understand the flow of your code.
A detailed conversation about the Express vs Hapi goes here.

If you managed to reach here, I guess you liked the article. So how about recommending this article, so that others could also waste their 8 mins. :D