How does Node.js compare to a traditional MVC platform like Spring?

By: ; Date: 2016-04-27 23:07

Tags: Node.JS » Java » Spring

Node.js is a young software development platform. It's only about 6 years old, and many software developers are still unsure about where Node.js fits into the landscape. It's JavaScript, which many people pigeon-hole as a browser language, but Node.js doesn't run on browsers but on servers. The question above illustrates a fundamental confusion about what Node.js is, and its role in the world, because it is not an "MVC" but a complete programming platform.

What is Node.js? It's a general-purpose programming platform using JavaScript as the language, and a runtime full of asynchronous file-system and network I/O functions. It supports an event-oriented model of programming, using JavaScript features like anonymous callback functions and Promise objects to their fullest. It is a lightweight system that eschews traditional thread-based architectures, and instead relies on a single threaded event-loop architecture. The combination produces a system that can sustain very high transaction throughput, and is very easy to program for.

Spring is kind of the antithesis to Node.js, and it may be that Node.js's popularity is due to many programmers tired of the over-the-top nature of the Spring ecosystem. What I mean is that Spring throws in dozens of kitchen sinks worth of features to make an extremely complicated system that tries to be the be-all-end-all. To be fair, many of the kitchen sinks I refer to are not Spring per-se but other projects like Hibernate and Java Persistence. Through Spring DAO objects, a specific MVC (Model View Controller) model is strongly encouraged. The system is written in Java, on Java EE appservers like Tomcat or Glassfish. These appservers are themselves large hairy beasts full of complexity, and strongly dependent on a thread-based architecture.

Maybe that characterization of Spring will offend some. Consider that 10+ years ago I worked at Sun in the Java SE team, and was routinely writing blog posts on my java.net blog defending Java as being superior to JavaScript. Today my opinions have flipped because of my experience with Node.js, and the ease of programming it offers. I currently am employed writing Spring code ... the other day I needed to return a JSON object to an HTTP POST request, and therefore needed to create a JSON Mapper object -- because -- uh -- Spring makes you do all kinds of excess work to get anything done. Namely, I needed to create a Java class with a couple dozen fields. After using Eclipse to auto-generate the Constructor, the Getters and Setters, and the hashCode and toString and equals methods, the class weighs in at almost 600 lines of code. All that just to manage a couple dozen fields.

By contrast in Node.js the same task - returning JSON from an HTTP POST request - would take a handful of lines of code. Rather than be burdened with implementing a 600 line monstrosity just to return some data, in Node.js you simply pass an anonymous object (because JavaScript supports such things) to a method that formats objects as JSON, and QED the thing is done as quick as can be.

This was a slight detour from answering the question, but it points in the direction I want to take. Namely - Node.js is full of low computational cost solutions that are easy to code, easy to maintain, and straightforward. Spring, on the other hand, is full of heavy-weight solutions because what seems to me as a mindset that Spring (and the whole Java ecosystem) goes beyond the pale to solve every last problem that exists.

Node.js does not have any opinion about how you organize your code. It doesn't care where the source files are located, what's in them, whether you define proper classes, whether you use anonymous objects, whether the capitalization of method or variable or class names is correct, whether you use the model-view-controller paradigm, and on and on and on. Its role is to simply provide a base set of runtime modules, and an excellent JavaScript engine sourced from the Google Chrome team. You make of Node.js what you will.

Spring forces you to use Java Beans DAO objects, and to organize HTTP responses around Controller classes, to use lots of XML files to describe things that should be written in code, and on and on. The Java Bean model relies on Java annotations that perform magic functions where you write a "simple" Java class sprinkled with annotations. The system then magically instantiates these Beans somehow and on and on. It's really nauseating thinking about how many layers of abstraction are going on, and the computational/complexity overhead entailed by something as innocuous-seeming as the @Autowired annotation. How does Spring magically figure out how to find the value it is autowiring into place? And why is it so hard to just explicitly write the code in the first place?

There are MVC frameworks written on top of Node.js. The better question would have been - how does Express (or another framework) compare against Spring? But even that's not good since Express itself is not MVC-oriented.

In any case, the frameworks available for Node.js tend to be streamlined, easy to program, straightforward, etc. The JavaScript language helps a lot with this since it is so much easier to program in JavaScript than in Java.

Way-back-when, what was my argument that Java is superior? The Java language actively helps you catch certain kinds of errors because of strict type-checking of all kinds of things. Fields in objects, method parameters, and basically everything else you touch has to be rigidly and correctly typed. In JavaScript you're using anonymous objects and anonymous functions all over the place, with no data typing, and it would seem like a nightmare of potential problems.

But the impact of strict typing is an onerous burden of coming up with data types for everything you touch. What if you have two methods communicating data -- data that will ONLY be exchanged PRECISELY between these two methods? In Java you still have to define a Class to store that data, and depending on the shape of that data the Class might be complex. In JavaScript you simply throw the data into an anonymous object, and make sure the two functions are written correctly to deal with the object. An example is the JSON Mapper I mentioned earlier -- 600 lines of code to manage an object with 20 fields. Bah.

In Express/Node.js it's this simple

res.send({
  field1: value1, field2: value2, field3: value3, ...
});

The result is the same - JSON sent to the caller. Which is easier to write, easier to maintain, easier to understand?

« Is Node.js / Express scalable? How to grow Node.js app deployment to handle more traffic? Node.js 4.0.0 is out - quick tip for use while testing compatibility »
2016 Election Acer C720 Ad block AkashaCMS Android Apple Apple Hardware History Apple iPhone Hardware April 1st Arduino ARM Compilation Asynchronous Programming Authoritarianism Automated Social Posting Bells Law Big Brother Blade Runner Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Civil Liberties Clinton Cluster Computing Command Line Tools Computer Hardware Computer Repair Computers Cross Compilation Crouton Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Digital Photography DIY DIY Repair Docker Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD Election Hacks Emdebian Enterprise Node ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Home Automation HTTPS I2C Protocol Image Conversion Image Processing ImageMagick InfluxDB Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Joyent Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Missy Mobile Applications MODBUS Mondas Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea Online advertising Online Fraud Open Media Vault Open Source Software OpenAPI OpenVPN Personal Flight Peter Capaldi Photography Plex Media Server Political Protest Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Republicans Retro-Technology Reviews Right to Repair River Song Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Scheme Science Fiction Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spring SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Ubuntu Virtual Private Networks VirtualBox VLC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Weeping Angels WhatsApp Wordpress