Chris has been married to the love of his life, Danielle, for over four years now and they recently had their first child, their daughter Helena. Chris was an expert wrangler of developer content for DZone, but recently started creating marketing content for DZone and AnswerHub. If you're looking for someone to bore you to death with endless football knowledge, Chris is your go-to-guy! Chris is a DZone employee and has posted 286 posts at DZone. You can read more from them at their website. View Full User Profile

From Java to Node.js

01.10.2012
| 14259 views |
  • submit to reddit
I’ve been developing for quite a while and in quite a few languages.  Somehow though, I’ve always seemed to fall back to Java when doing my own stuff – maybe partly from habit, partly because it has in my opinion the best open source selection out there, and party because I liked its mix of features and performance.

Originally authored by Matan Amir


Specifically though, in the web arena things have been moving fast and furious with new languages, approaches, and methods like RoR, Play!, and Lift (and many others!).  While I “get it” regarding the benefits of these frameworks, I never felt the need to give them more than an initial deep dive to see how they work.  I nod a few times at their similarities and move on back to plain REST-ful services in Java with Spring, maybe an ORM (i try to avoid them these days), and a JS-rich front-end.

Recently, two factors made me deep dive into Node.js.  First is my growing appreciation with the progress of the JavaScript front-end.  What used to be little JavaScript snippets to validate a form “onSubmit” have evolved to a complete ecosystem of technologies, frameworks, and stacks.  My personal favorite these days is Backbone.js and try to use it whenever I can.

Second was the first hand feedback I got from a friend at Voxer about their success in using and deploying Node.js at real scale (they have a pretty big Node.js deployment).

So I jumped in.  In the short time I’ve been using it for some (real) projects, I can say that it’s my new favorite language of choice.

First of all, the event-driven (and non-blocking) model is a perfect fit for server side development.  While this has existed in the other languages and forms (Java Servlet 3.0, Event Machine, Twisted to name a few), I connected with the easy of use and natural maturity of it in JavaScript.  We’re all used to using callbacks anyway from your typical run-of-the-mill AJAX work right?

Second is the community and how large its gotten in the short time Node has been around.  There are lots of useful open source libraries to use to solve your problems and the quality level is high.

Third is that it was just so easy to pick up.  I have to admit that my core JavaScript was decent before I started using Node, but in terms of the Node core library and feature set itself, it’s pretty lean and mean and provides a good starting point to build what you need in case you can’t find someone who already did (which you most likely can).

So having said all that, I wanted to share what resources helped me get up to speed in Node.js coming from a Java background.

Getting to know JavaScript

There are lots of good resources out there on Node.js and i’ve listed them below.  For me, the most critical piece was getting my JavaScript knowledge to the next level.  Because you’ll be spending all your time writing your server code in JavaScript, it pays huge dividends to understand how to take full advantage of it.

As a Java developer you’re probably trained to think in OO designs.  With me, this was the part that I focused the most on.  Fortunately (or unforunately), JavaScript is not a classic OO language.  It can be if you shoehorn it, but i think that defeats the purpose.

Here is my short list of JavaScript resources:

  1. JavaScript: The Good Parts - Definitely a requirement.  Chapters 4 and 5 (functions, objects, and inheritance) are probably the most important part to understand well for those with an OO background.
  2. Learning Javascript with Object Graphs (part 2, and part 3) – howtonode.org has lots of good Node material, but this 3 part post is a good resource to top off your knowledge from the book.
  3. Simple “Class” Instantiation – Another good post I read recently.  Worth digesting.

Learning Node.js

With a good JavaScript background, starting to use Node.js is pretty straightforward.  The main part to understand is the asynchronous nature of the I/O and the need to produce and consume events to get stuff done.  Here is a list of resources I used to get up to speed:

  1. DailyJS’s Node Tutorial - A multipart tutorial for Node on DailyJS’s blog.  It’s a great resource and worth going through all the posts.
  2. Mixu’s Node Book - Not complete, but still worth it.  I look forward to reading the future chapters.
  3. Node Beginner Book – A good starter read.
  4. How To Node – A blog dedicated to Node.js.  Bookmark it.

I felt that going through these was more than enough to give me the push I needed get started.  Hopefully it does for you too (thanks to the authors for taking the time to share their knowledge!).

Frameworks?

Java is all about open source frameworks.  That’s part of why it’s so popular.  While Node.js is much newer, lots of people have already done quite a bit of heavy-lifting and have shared their code with the world.  Below is what I think is a good mapping of popular Java frameworks to their Node.js equivalents (from what I know so far).

Web MVC

In Java land, most people are familiar with Web MVC frameworks like Spring MVC, Struts, Wicket, and JSF.  More recently though, the trend towards client-side JS MVC frameworks like Ember.js (SproutCore) and Backbone.js reduces the required feature-set of some of these frameworks.

Nonetheless, a good comparable Node.js web framework is Express.   In a sense, it’s even more than a web framework because it also provides most of the “web server” functionality most Java developers are used to through Tomcat, Jetty, etc (more specifically, Express builds on top of Connect)  It’s well thought out and provides the feature set needed to get things done.

Application Lifecycle Framework (DI Framework)

Spring is a popular framework in Java to provide a great deal of glue and abstraction functionality.  It allows easy dependency injection, testing, object lifecycle management, transaction management, etc.  It’s usually one of the first things I slap into a new project in Java.

In Node.js… I haven’t really missed it.  JavaScript allows much more flexible ways of decoupling dependencies and I personally haven’t felt the need to find a replacement for Spring.  Maybe that’s a good thing?

Object-Relational Mapping (ORMs)

I have mixed feelings regarding ORMs in general, but it does make your life easier sometimes.  There is no shortage of ORM librares for Node.js.  Take your pick.

Package Management Tools

Maven is probably most popular build management tool for Java.  While it is very flexible and powerful with a wide variety of plug-ins, it can get very cumbersome.  Npm is the mainstream package manager for Node.js.  It’s light, fast, and useful.

Testing Framework

Java has lots of these for sure, jUnit and company as standard.  There are also mock libraries, stub libraries, db test libraries, etc.  Node.js has quite a few as well.  Pick your poison.  I see that nodeunit is popular and is similar to jUnit.  I’m personally testing Mocha.  Testing tools are a more personal and subjective choice, but the good thing is that there definitely are good choices out there.

Logging

Java developers have quite a list of choices when choosing a logger library.  Commons logging, log4j, logback, and slf4j (wrapper) are some of the more popular ones.  Node.js also has a few.  I’m currently using winston and have no complaints so far.  It has the logging levels, multiple transports (appenders to log4j people), and does it all asynchronously as well.

Hopefully this will help someone save some time when peeking into the world of Node.  Good luck!



Source:  http://n0tw0rthy.wordpress.com/2012/01/08/from-java-to-node-js/