View profile

Why I Build APIs with Node.js

Right now the most popular backend languages seem to be JavaScript (via Node.js), Python, Java, PHP,
Why I Build APIs with Node.js
By Mastering JavaScript Tutorials • Issue #21 • View online
Right now the most popular backend languages seem to be JavaScript (via Node.js), Python, Java, PHP, Golang, and C#. There’s a lot of debate as to which one is best. But, as you might expect, Node.js is the best choice in my opinion. Here’s why.

Event Loop
The major difference between JavaScript and the other languages on the list is that JavaScript’s concurrency model is based on the event loop rather than threads. Threads have been the de facto concurrency model since the 1960’s, but they were designed for a very different type of computing than what most developers work on today.
Threads were primarily designed to support scientific computing, ensuring that all researchers got to run their computations without blocking each other. Professor Smith could run his planetary motion simulator at the same time as Professor Jones was testing out his red-black tree implementation. The key problem was that these programs both required the CPU at all times in order to do computations, so the operating system needed to have two “threads” of execution to ensure that both programs got to access the CPU.
Fast forward to 2020, and most web developers barely use the CPU at all. Say you have a Node.js script that makes an HTTP request to a remote server. Node.js uses a little CPU time to serialize the request, and then spends a lot more time waiting for the response. CPU is no longer a scarce resource because most web apps do minimal computation - they defer the work across the network to some other machine.
You could get faster JSON parsing by writing your web server in C rather than JavaScript. But even if you get 20% faster JSON parsing, it doesn’t matter if your average request handler spends 1% of its time parsing JSON and 99% of its time waiting for a network round trip to the database.
The event loop is a much better model for programs that are network constrained rather than CPU constrained. Instead of worrying about how many threads you need and how to ensure that your threads don’t waste resources when they’re waiting for an HTTP response, you just use await.
Naturally, other languages have libraries for event loops. Tornado is a common event loop based HTTP server for Python. C# has a built-in event loop framework as well. But those come with caveats - not all Python or C# libraries work well with event loops.
Code Portability and Composability
One underrated benefit of the event loop is how easy it is to combine multiple disparate projects into one process. For example, you can start a fully fledged web server and test it in the same process. No need to run your server as a background process, just `require()` your server and start using it.
Even starting multiple projects from one process is easy. List a GitHub repo as a dependency, `require()` in some code, run it. No need to set up a separate thread with a messaging queue. It just works.
Testing and Tooling
Because JavaScript is so composable, you can do some pretty fancy things with testing and tooling. One common trick is reusing the HTTP requests your client uses for testing your API - you can even use the same HTTP client as your client web app, because HTTP libraries like Axios are isomorphic. Set up a CLI for your web server? Easy. Write your own dev tools? There’s a blog post about that.
Most Recent Tutorials
Last week’s tutorials were all about Node.js. Check out the tutorial about uploading Lambda functions to AWS. You might find it easier to upload Lambda functions via API than via the AWS console.
Did you enjoy this issue?
Mastering JavaScript Tutorials

Mastering JavaScript Tutorials

If you don't want these updates anymore, please unsubscribe here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue