Don’t use Node(backend), unless you need asynchronous code

Written by derek7mc | Published 2018/05/17
Tech Story Tags: javascript | nodejs | backend-development | web-programming | concurrency

TLDRvia the TL;DR App

The simplest technology that meets your requirements will save you time and mental energy, leading to better problem solving where it counts

Node.js is an amazing technology. Its key innovation is an event loop based execution model that facilitates writing programs that run with non-blocking operations. Here is an article on infoworld explaining that idea.

When you write a server in nodejs, everything is call-back based, and the event loop coordinates execution on a single javascript thread.

Nodejs combines two important programming ideas: event driven programming, and concurrency, in a way that is conceptually accessible for programmers to handle. If you can understand the following code, you can start writing programs in nodejs:

<html><head><script>document.write("Round and round the cobbler's bench,<br>");document.write("The monkey chased the weasel.<br>");document.write("The monkey thought 'twas all for fun<br>");setTimeout(surprise, 4000);function surprise(){alert("POP!");document.write(". . . goes the weasel.");}</script></head></html>

Rich satire on imitation and IP

Programming is all about building up previous ideas and performing them in a new and creative way. And that’s exactly what nodejs did. It took a messy and inconsistent language designed for web browsers, and, using google’s v8 engine, made it one of the most powerful and performant server technologies of that time.

Over the past decade, javascript has risen from barely breaking the top 10 list of programming languages, to being, by many accounts, the #1 programming language to learn in 2018:

Here are the best programming languages to learn in 2018_This is the definitive guide for anyone wanting to choose the right programming language career path in 2018._medium.freecodecamp.org

And I 100% agree that learning javascript is a great idea.

But just because you should learn javascript, doesn’t mean you should use nodejs for your webserver. There are other reasons to use nodejs, or javascript. Node.js is the center of a huge ecosystem of open source tools, and javascript is a language that embraces simplicity and flexibility, even though it gets messy sometimes.

When programming servers in nodejs, callbacks add complexity and can be a source of confusion, even when you are careful and design your program well. I would only recommend using nodejs as a server if the asynchronous non-blocking design is a critical part of the requirements of your system.

What are some simpler alternatives for programming web servers?

  • PHP and MySQL
  • CGI with Python or Perl
  • Bottle.py / SimpleHTTPServer
  • Mojolicious / CherryPy / Sinatra
  • Django / Ruby on Rails
  • GoLang (still need to understand concurrency, but only have to use it when you want to)

I consider each of these tools easier ways to write and maintain webservers in the long run than using nodejs. That’s my opinion and many may disagree with me, but it’s worth it to get familiar with your options and the tradeoffs involved. Notably, golang is the only strongly typed language that makes this list. GoLang has its own challenging concepts, but after the initial learning curve, I think it allows you to program simply when you want to, while the callback model of nodejs changes the way you have to think about programs.

It’s actually easier, in my opinion, to learn a new language than to master a new paradigm, ecosystem, and framework all at once. Everything in the nodejs server ecosystem has to be written asynchronously, because the threading model and good design make it a bad idea to combine synchronous and asynchronous operations. Async/await can improve clarity, but under the surface, you still need to understand what is happening asynchronously, as if it were written with callbacks. Otherwise, you may be hurt by the law of leaky abstractions. Asynchronous programming adds complexity and conceptual overhead to every step of the programming process, so be careful when you choose to use it.

If you decide to write your application in nodejs, it will probably take you more work than some of these other tools, but sometimes it is worth it.

There is only one example I can justify, where I decided to use nodejs only because it is “Server javascript”. I decided to write a framework for online turnbased browser games, and it was important to me that developers could use the same language for defining the server logic as writing their games in the browser. “Server javascript” should normally be the last reason you choose to use nodejs.

While the concepts of nodejs are accessible and powerful, I think they are a more challenging way to design and build web applications in the long run, and may increase the difficulty of maintaining your web applications.


Published by HackerNoon on 2018/05/17