CODEX

Introduction to Node.js

Kasuni Madhushika
CodeX
Published in
3 min readMar 21, 2021

--

Node.js logo
Figure 1: Node.js Logo

Node.js is a server-side, open-source and cross-platform JavaScript runtime environment. This built on Google Chrome’s JavaScript Engine (V8 Engine). The definition of Node.js is as follows ;

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

This systems are developed using JavaScript. We can use Microsoft Windows, Linux and OS for run these applications. As well as this provides various libraries for web applications. We can use “npm” (Node Package Manager) for install these libraries.

Features of Node.js

There are some important of Node.js

  1. Asynchronous and Event Driven : In the Node.js, all the API libraries are asynchronous. Asynchronous means that the client never wants to wait till the data return message. After calling that, the server moves to another API and Node.js helps the server to get a response from the previous API call.
  2. Single Threaded but Highly Scalable : Node.js uses a single threaded programming language. Event mechanism helps the server to respond in a asynchronous way. Then it makes the server highly scalable create limited threads to handle requests. Since it uses a single threaded programming language it can provide service to a much larger number of requests than traditional servers.
  3. No Buffering : These applications simply output the data in chunks rather than buffer.
  4. Very Fast : Node.js libraries are very fast in code execution.

Node.js Concepts

In the following image we can see all of the using Node.js concepts.

Node.js Concepts
Figure 2: Node.js Concepts

Where to Use Node.js?

  • Single Page Applications
  • I/O bound Applications
  • Data Intensive Real-time Applications (DIRT)
  • Data Streaming Applications
  • JSON APIs based Applications

Where Not to Use Node.js?

  • CPU intensive applications

Create an application

There are some components before creating the application.

  1. Import required modules : Use the require directive to load Node.js modules. For this create a file called index.js in your terminal. You can use Visual Studio Code or WebStorm for this.
var http = require("http");

2. Create server : Similar to Apache HTTP Server. In here we use http instance and call “http.createServer()” method to create a server instance and bind it at port 8081 using the listen method associated with the server instance. Then pass a function with parameters request and response.

http.createServer(function (request, response) {response.writeHead(200, {'Content-Type': 'text/plain'});

response.end('Hello World\n');
}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');

3. Read request and return response : The server created and read the HTTP request made by the client and return the response.

var http = require("http");

http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});

// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

Then we can execute the index.js file. In the terminal simply execute this code.

$ node main.js

You can see the output in the server. Open http://127.0.0.1:8081/ in any browser and observe the following result.

Figure 3 : “Hello World!”

References

You can refer Node.js official website for more information.

https://nodejs.org/en/docs/

--

--

Kasuni Madhushika
CodeX

Software Engineering Undergraduate at Sri Lanka Institute Of Information Technology