What Is Node.js
Node.js is a runtime environment for JavaScript. If you’ve tried hosting Node.js apps, you probably know most of this. However, beginners are usually a bit confused as to what Node actually does and how it differs from other server-side scripting technologies.
Let’s start with JavaScript and see how Node factors in.
In the Beginning, There Was JavaScript
If you’re unfamiliar with it, JavaScript is a programming language typically used to add functions to a web page. It’s omnipresent on the Web and pretty much necessary to build web pages. All pages on HostingTribunal.com use it, and you’d be hard-pressed to find any website that doesn’t.
However, JavaScript can’t really do much on its own. I could send you a full script, but computers aren’t equipped to run it by default.
To actually make JavaScript work, you need a browser. Each browser has a runtime engine that lets it execute JS code and present you with a nice, functional web page.
And delivering JavaScript to the client’s browser was the only way you could execute it till 2009. It was exclusively a frontend language.
How Node.js Changed Things
Enter Node.js.
Node is something like an adapted version of Google Chrome’s V8 engine. You can install on a server and execute JavaScript code in the backend. If you deploy a Node.js app to a server, you’re actually writing a website backend with JavaScript.
Most people might wonder why you’d introduce a new way of writing backend code. After all, many programming languages already perform the same functions. Moreover, some, like Java or PHP, had massive libraries and were tried and tested before Node.js showed up.
You can read about this in more detail in the next section. In a nutshell, Node.js basically helps the backend get out of its own way by allowing asynchronous I/O. Other languages typically waste some server resources performing input/output operations, while JavaScript doesn’t have that problem.
The end goal is to create a much leaner and faster backend, which means you spend less money on hosting—and who doesn’t want that?
So, let’s cover when you should go for Node.js hosting.
What Node.js Is (and Isn’t) Good for
Using Node.js to write I/O-heavy apps is a logical choice. It is blazing-fast here, and many companies have easily halved resource consumption just by going from Java or other languages to Node.js.
Node.js is also awesome for building real-time communication programs. Almost everyone is familiar with messenger apps, which are the perfect example. These feature frequent data transfer between multiple devices, but there’s little data processing involved. The server mostly deals with receiving and sending data.
Another function where Node.js excels is streaming. Most people think about video or audio streaming, but it can really be any type of data.
Building any sort of dashboard for real-time system monitoring is also a breeze with Node.js. It’s just another example of an app that’s heavy on the I/O and easy on the CPU.
There are plenty more reasons to host Node apps, but you’re most likely to run into or use the ones above.
Of course, Node.js isn’t always the perfect solution. There are situations where it’s better to use another language.
A thing to understand about Node is that it’s meant to help in cases where heavy input/output is bottlenecking the CPU. If you want to run a CPU-intensive app, you’re missing out on the advantages of Node. What’s more, this can actually reverse the whole thing, and your CPU can become the bottleneck for the entire system.
That’s really the gist of it. If you are running something CPU-heavy, like an image editing app, Node.js can do more harm than good. In such cases, it’s better to go with the multi-threaded model.
Otherwise, Node.js server hosting can be a gamechanger for your apps.
If you’d like to know more about the nitty-gritty of how Node.js works, read on.
How Node.js Works
If you have some experience with Node.js, you’re most likely familiar with the single-threaded event loop and how it works. It can be a bit confusing for most users, though, even those with JavaScript experience.
Let’s start with the traditional model and then explain how hosting Node.js apps changes things. Most web applications feature a multi-threaded request-response architecture, so it’s the standard out there.
Traditionally, a client makes a request to the server. The server then spawns a CPU thread and reserves server resources to process the request as well as the response. The resources remain reserved until the response is processed.
A common problem here is that the CPU can do its part really quickly. However, it takes the API much longer to write or read data from the server memory. So, the CPU and RAM allocated to the thread are just sitting there until they can process the response to the client.
Think about it this way. Imagine you’re running a coffee shop. Any time a customer comes in, a waiter takes their order, waits for the barista to make the coffee, then takes it to the customer. Pretty simple.
But let’s say multiple customers come in at the same time. Either they would have to wait an inordinate amount of time, or you’d hire multiple waiters (spawn multiple CPU threads). Still, this doesn’t solve the problem of having a bunch of idle waiters while coffee is being made.
Why wouldn’t a waiter go around taking orders until the coffee for the first customer is ready? You could have one waiter serving dozens of customers at a time.
As you can probably guess, this is where running (and hosting) Node.js applications changes. Instead of having multiple threads waiting for the API, all CPU and RAM is fully available to a single thread that runs non-blocking functions.
It all goes toward processing requests and responses. If the API needs time to, let’s say, retrieve something from the database, the processor schedules a callback and continues processing other requests. Once the data is ready, the application can go back and process the response. This is known as asynchronous I/O (or non-blocking I/O).
This way, you can have thousands of requests processed on a single CPU thread. Since you’re using full CPU and RAM capacity without wasting any of it, you can process requests much faster.
Now, this is a gross oversimplification. There are lots of technical details that go into the single-thread event loop. Still, this is the gist of how it works differently from other backend technologies.
What to Consider when Looking for a Node.js host?
A few factors go into consideration when picking the best Node.js hosting provider.
Will Node.js Hosting Work for Your Application?
Node.js is an excellent tool and is a godsend for many websites. Still, it’s no panacea for all web hosting ills. While it improves some apps, it can break others.
You can see from the previous section why Node.js works well for data-heavy apps.
When visitors make requests that require input/output operations, it tends to slow down traditional apps. Node.js can process thousands of requests without reserving gigabytes of RAM until server-side web API comes through. In other words, it’s perfect if heavy I/O is bottlenecking your processor.
It’s less than perfect if you want to build a CPU-intensive app.
If you have to do heavy processing for numerous clients on only one thread, your CPU actually becomes the bottleneck. Sure, the first few clients in the queue will get their responses quickly enough. The rest will have to wait a long time, though, and that’s something you can’t have on the Web.
The multi-threaded model handles heavy processing much better.
In short, whether you need web hosting for Node.js or another technology depends on the web application you want to build
Will Node.js Work for You as a Developer?
Many developers are attracted to Node.js because it uses JavaScript code.
You can’t really blame them. JavaScript is probably the most popular programming language. Also, handling application development end-to-end in a single language is undeniably attractive. You don’t have to know multiple languages, cooperation between developers is easier, everything integrates well—the benefits go on and on.
However, a common pitfall is assuming writing JavaScript backend code is the same as coding in the frontend.
Yes, it’s easier to learn Node.js if you’ve used JavaScript before. That said, there are a lot of peculiarities about server-side scripting with JavaScript, like creating custom events. Plus, it takes time to get the hang of all the tools in the Node Package Manager.
This is not meant to discourage you. It’s just important to know there’s more to it than finding the right Node.js server host, even if you’re proficient in JavaScript.
Still, you’ll probably find it a lot easier to learn Node than other languages. Being familiar with the key concepts of backend web development is also a great help.
Of course, this doesn’t apply if you’ll have the website built for you. In such cases, you should definitely consult your developer and see if Node.js is the best solution.
So, if you have the time to commit to Node.js, let’s get into actually picking the provider.
Shared, VPS, or Cloud?
Many Node.js devs start with a blank, unmanaged Node VPS and build everything from the ground up.
This can be pretty daunting if you’re starting out. A managed solution can reduce your workload, as well as be something to fall back on if you find yourself in a pinch.
While a few solid shared hosting options exist, these are generally limited for running Node.js.
One reason is that shared hosting usually entails somewhat limited resources.
A more significant factor is that shared hosting can be restrictive when it comes to supported technologies. Any change to the server affects all shared users, so hosts have to be selective about what they allow. For instance, it’s pretty much impossible to run MongoDB with shared hosting, even though it’s a popular database program for Node.js users.
Still, if you can find the right fit, shared plans can be an inexpensive way to get started or just do some testing.
Node.js VPS hosting plans are usually much more suitable. These servers are usually fully customizable, so you can fine-tune the servers to the JavaScript runtime environment. Additionally, they offer more resources, while staying relatively inexpensive. In essence, VPS is a perfect midrange option for Node.js.
Many Node.js developers have taken a liking to the cloud, and cloud hosts have responded. Dozens of cloud hosting providers offer a multitude of Node.js-optimized platforms.
Fully customizable cloud solutions add unmatched reliability and durability. Plus, these platforms are insanely scalable, ensuring you don’t waste resources, which is what Node.js is all about. If you want a powerful provider for powerful, scalable applications, Node.js cloud hosting can provide.