Basic Node Js Application Code

Posted on  by admin
  • This Node js tutorial is designed for beginners to help you learn Node.js step by step.
  • All you need to do is follow this Node.js tutorial stepwise.
  • Each step covers important topics related to Node.js.

This Node.js tutorial is divided into 7 steps. We expect you to follow this step by step. We are going to cover the following topics in this Node.js tutorial:.

  • Step 1: Node js basic concepts – Libuv, Event loop, Libev. Step 2: Building a Simple Web Server in Node.js.

  • Step 3: Node.js modules and NPM. Step 4: File system module. Step 5: Express framework.

  • Step 6: Databases – MySQL, MongoDB, PostgreSQL and Redis. Step 7: Deployment – deploying applications in Digitalocean Server. After reading this Node.js tutorial, you should be able to build applications using Node.js and deploy it on a cloud server.

Step 4: File system module.

Deploy your application

Node.js runs on top of V8—Chrome runtime engine—that compiles the JavaScript code in the native machine code (one of the reasons why Google Chrome runs fast and consumes a lot of memory), followed by the custom C++ code.

Step 5: Express framework

The original version has 8,000 lines of code (LOC)—and then, the standard libraries for programmers. The following is the figure of Node.js architecture:. The V8 JavaScript engine is an open-source JavaScript engine developed for the Chrome project.

The innovation behind V8 is that it compiles the JavaScript code in native machine code and executes it.

Step 1: Node js basic concepts

The developers of V8 used the just-in-time (JIT) compiler methodology to improve the code compilation time.

It is open-source and is used in the Node.js and MongoDB project.


An Express application

The libuv library is a cross-platform library that provides an asynchronous I/O facility by enabling an event-driven I/O operation.

The libuv library creates a thread for the I/O operation (file, DNS, HTTP, and so on) and returns a callback.

Step 7: Deploying Node application to a Cloud Server

Upon completion of the particular I/O operation, it returns the events so that the callee program does not have to wait for the completion of the I/O operation.

Event driven I/O – libuv

As I mentioned earlier, libuv assigns threads for the I/O operation and returns the callback to the callee program.

Therefore, Node.js internally creates threads for I/O operation; however, it gives the programmer access to a single runtime thread.

In this way, things are simple and sweet:.

Running Hello World

When you make an HTTP request to the web server built using Node.

It creates the libuv thread and is ready to accept another request.

As soon as the events are triggered by libuv, it returns the response to the user.

Working of libuv – the core of Node.js

The libuv library provides the following important core features:.

Fully featured event loop.

Asynchronous filesystem operations.

How Event Loop Works

Thread and synchronization primitives.

Asynchronous TCP and UDP sockets.

Signal handling.

error-handling middleware

The libuv library internally uses another famous library called libeio, which is designed for threading and asynchronous I/O events and libev, which is a high-performance event loop.

Therefore, you can treat libuv as a package wrapper for both of them.

Let’s learn a little bit about multi-threading and single threading.

Application middleware

The multi-threading approach provides parallelism using threads so that multiple programs can simultaneously run.

it is really difficult to handle concurrency and deadlock in a multi-threading system.

On the other hand, with single-threading, there is no chance of deadlock in the process, and managing the code is also easy.