This article gives an overview of how Node.js works under the hood along with some pointers to get started with building your first Node.js application.
The Browser loads the script, compiles it on the fly to binary code and executes it.
The need to have consistent codebase across client and server side was growing, therefore a runtime similar to the one running in Browsers was required for servers.
In addition to being a compiler and an optimiser, this runtime has to manage the dependencies between modules and to handle the events received from the client side and the callbacks.
It is also famous for being fast and performant compared to similar runtimes.
Below some if its main features:
- Single Threaded: It is single threaded, unlike other server side runtimes (e.g. Vertx). Node.js maintains a queue of requests from the client side, processes one request at a time and sends back the response. This makes it performant because the cost of spinning new threads for new incoming requests can be expensive, This is because a large block of memory has to be allocated and initialised for the thread stack, it needs to be registered in the runtime, which makes it CPU intensive.
- Non-blocking: It offers the possibility to have non-blocking code. All I/O operations in Node.js have async versions with callbacks. This is useful in the context of single threaded event loop; we don't want to block the next request to be processed by having blocking code.
Building a Node.js application
Now that we have covered the main features offered by Node.js, we are ready to start building our Node.js server side application. The internet is filled with simple tutorials to build such applications!
Here is a list of the most used modules to build a MERN stack application, MERN stands for MongoDB-Express-React-Node.
- webpack: helps with building and packaging the code into a bundle file, which is compatible and readable by the browser.
- fetch: provides an interface to make HTTP requests.
- express: is a web framework for Node.js.
- mongoose: is a mongodb object modelling for node.js.
- react: is the React library to build user interfaces.
Now that you had a sneak-peak of Node.js main functionalities, let's dive into some programming. Here is official getting started guide that I find really helpful for newbies. Happy coding!