Who likes to go out to a restaurant to eat every once and a while?
It’s nice to treat your significant other or buddies to a restaurant meal every now and then, but the idea soon becomes a bad one if the service is slow or lagging – you end up with a table full of grumpy, empty-bellied guests who eventually threaten to leave.
Just like hungry guests who have waited too long can leave the restaurant, requests can “time out” if the server takes too long to process them.
Enter Node.JS and the 5 things you need to know about it in 2021!
Everyone’s actually eaten at a restaurant, right? A customer walks in, and they’re seated by a waiter who takes their order and notifies the kitchen to prepare the meal – before bringing it out to them when it’s ready.
Once the order (request) is taken from the customer (client) by the waiter (event loop), he takes the order to the kitchen pass (event queue) where it’s assigned to a chef (callback). Think of the chef as the access to the files on the server, who controls the food storeroom (input), as well as which ingredients and portion sizes make up the dish (output).
However, ordering food supplies is ultimately the responsibility of the restaurant owner (the operating system). The waiter (event loop) constantly checks the kitchen pass (event queue) to see if the order (request) is ready, so they can then take it to the customer’s (client’s) table.
If the customer orders right away, they take up hardly any of the waiter’s time so they can get right onto serving the next customer – just like non-blocking code that doesn’t increase the processing interval. But say that customer wants to ask the waiter about a particular dish, or to add something to an item on the menu, then this would require the waiter’s attention for a few more minutes – this is blocking code, a request that can only be processed once all the variables related to the request have been confirmed with the client. Think of it like an especially large file that needs to be read, with other tasks to be executed afterwards.
Likewise, packets of cutlery and pre-folded napkins drastically reduce the time the waiter needs to prepare the table and give the customer everything they need to eat their meal. In Node.js these are Node Packaging Managers (NPMs), a library of pre-written bits of code that can be installed to perform menial tasks without the need to write them from scratch.
But what about if you hire a general manager to oversee the day to day running of the restaurant, and makes sure that things run smoothly and all customers are served in both a timely fashion, and are happy with their meals? You’d be looking at Express.js.
Sure, the waiter takes care of the order, service, and paying of the cheque, but it’s the general manager who’s making other decisions like if the customer is adequately dressed to dine at the restaurant, or if customers are over 21 and can be served alcohol – just the same as Express.js handles things like login credentials and whether the request comes from an actual account with the correct password.
The customer prefers to order appetizers, main course, desert, and coffee all at the same time, so both the waiter and the chef need some guidance from the general manager of the restaurant to help with the timing of the courses. This is exactly how Express.js functions as a layer of middleware within Node.js that creates that little bit of abstraction for the client user to issue customized requests to the server.
The general manager would also be able to make judgement calls on whether to seat people at the bar, inside at a table, or on the outdoor terrace based on how many people are in the party, whether they’re ordering main courses or light appetizers while having a glass of wine.
Node.js which would just have a single waiter serving all customers, scaling poorly as demand rises. Even so, that waiter is the best in the world, and easily outworks 5 regular waiters – just how Node.js is often more efficient than other multi-threaded systems with just a single thread.
The Express.js layer can hire more wait staff and more chefs in the same way that it allows Node.js to become multi-threaded, even if it is somewhat restricted with numbers.
1- What Is Node.js:
Basically, Express.js adds a set of rules that need to be applied and satisfied before any requests can be made to the server.
2-What Node.js is Used For:
Before Node.js, web-based apps incorporated a client/server model – simple enough, the client would request resources from the server, and the server would respond by providing the client with those resources. Each request was allocated processing and memory, and once completed, the connection was closed to free up those system resources to complete a different request etc.
Being completely event driven and running code based on callbacks from the event loop system, Node.js is available to perform other requests while waiting for previous ones to be completed without causing much of an increase to the processing delay of the event queue.
Node.js is based off the Chrome V8 Runtime engine, which is the same one the Chrome browser uses. Both Node.js and V8 are written in C programming language, which makes it super quick – much faster than others like Ruby or Python.
Node.js can cope with thousands of concurrent connections and requests as a single process thread, which isn’t very resource hungry and needs very minimal overheads from what may be a resource poor system with aging server hardware.
Easy To Learn:
Remember how Node.js just has the single, awesome waiter? Well, a single CPU core has a limit to how many requests it can handle at a time, so unless you have an Express.js layer, Node.js won’t make full use of system resources like a multi-core CPU – which is the norm in even the most basic home computers, let alone big server racks. Then again that single thread does out-perform a lot of multi-threaded back ends.
Node.js hates relational databases, we thoroughly recommend you never mix the two as they go together just about as well as oil and water.
However, we will add that there is an argument that you can use relational databases with node.js but the main issue is that it isn’t popular within the community so you would lack support in your endeavors. For the brave soul feel free to venture out and try it, but we warn you it may be a laborious task.
Stick To the Server:
Node.js is best suited for something like a web server, where it has to manage the input and output of stored data. Node.js isn’t great at CPU-intensive tasks because it itself is CPU-intensive by its very nature.
5- Which Companies Are Using Node.js:
It’s not surprising to see a whole bunch of highly successful and instantly recognizable companies using Node.js, with a real list of who’s who across multiple industries that includes LinkedIn, Netflix, Uber, Trello, PayPal, NASA, eBay, Medium, Groupon, Walmart… the list is as long as your leg really.
HOW TO GET STARTED?
Arguably the most all-encompassing Node.js resource on the web, and the go to for all novices.
This is the course to complete if you want to know more about how Express.js layers across Node.js, and how they function with other database systems.
This course is a little different in that it focuses on the strategy behind using Node.js to modularize your applications.
Kofi Group is proud to be a source of knowledge and insight into the startup software engineering world and offers a multitude of resources to help you learn more, improve your career, and help startups hire the best talent. If you are interested in learning more about what we do and how we can help you then get in touch or watch our Youtube videos for additional information.