Node Js Sample

Posted on  by admin

Node.js Examples : We shall go through examples of basics, fs module, mysql module, http module, url module, parsing json, etc. Following is the list of Node.js Examples. Following is a simple Node.js Example to print a message to console. Following is Node.js Example where we create a Calculator Node.js Module with functions add, subtract and multiply.

Step 3: Node modules and NPM

And use the Calculator module in another Node.js file. Following Node.js Example creates a file with data provided. Run the program using node command in terminal or command prompt :. The file should be created next to your example node.js program with the content ‘Learn Node FS module’.

Run the program using node command in terminal or command prompt :. make sure there is a file named ‘sample.txt’ next to the node.js example program. Run the program using node command in terminal or command prompt :. The file is successfully deleted. In this example, we shall write content, “Hello !” , to a text file sample.txt.

When the above program is run in Terminal,. We shall apply a filter based on marks and fetch only those records with marks greater than 90. Open a terminal from the location of above .js file and run selectFromWhere.js Node.js MySQL example program.

An example to sort entries in ascending order w.r.t a column. Run the above Node.js MySQL ORDER BY example program. The records are sorted in ascending order with respect to marks column. Run above Node.js MySQL program in Terminal. Run the above program in Terminal.

Step 6: Databases – MySQL, MongoDB, PostgreSQL and Redis.

Execute DELETE FROM query on specified table with filter applied on one or many properties of records in the table. We can access the records in Result Set as an array and properties of a record using DOT (.) Operator.

Run the above program using node in Terminal. Following example helps you to use JSON.parse() function and access the elements from JSON Object. Node.js Example – A HTTP Web Server that prepares a response with HTTP header and a message.

Open a browser and hit the url, “”, to trigger a request to our Web Server. 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. 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 2: Building a Simple Web Server in Node.js

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. 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.


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. 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.

NodeJS Example – INSERT entries INTO Table

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:. 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. The libuv library provides the following important core features:. Fully featured event loop. Asynchronous filesystem operations. Thread and synchronization primitives. Asynchronous TCP and UDP sockets. Signal handling.

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.

  1. 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. You can still hack and busy the event loop for no reason; however, that’s not the point.

Consider the following working diagram that is developed by StrongLoop—one of the core maintainers of Node.js:.