Node.js

Node.js “Hello World”. A brief introduction of Node js for beginners.

Node.js is an open-source cross-platform JavaScript runtime environment, particularly popular among web app developers. In this article we will run through the basic features on Node js as well as discuss how you can get started, including how to write a Node.js “Hello World!” program. We will also highlight some of the unique qualities of node js which have made it so popular among developers.

Let us explore more on what it means by Open source, cross platform and and a runtime environment.

Node js is an open-source that means everyone has right to use it, study it, change it, and even  distribute its source code to anyone and for any purpose.

Also, Node.js is cross platform so it can run on various platforms like Windows, Linux, Unix, Mac OS X, etc. As Node.js runs on V8 JavaScript engine, the core of Google Chrome.

Node js is neither a programming language nor a framework. Node js is a runtime environment.

Most people get confused on this subject. Please note Node js is neither a framework nor a programming language.

Programming language is a set of syntaxes and rules used to communicate with a computer device and instruct it to perform set of operations. Please do not confuse Node JS with a programming language. Although, it allows developers to use JavaScript on both server as well as client side, which is a programming language that allows users to build web applications but Node js itself is not a programming language.

Also, Node js is not a framework either because framework is an abstraction used by programmers to develop something. Usually framework includes pre-written code to help developers create software with ease, and its customisable as well. 

If Node js is neither a programming language nor a framework then what is Node js? Well, Node J is a Java Runtime Environment. A Java Runtime Environment (JRE) is a software that is made to execute other software. Node.js is used for building back-end services like APIs, Web App or Mobile App. It is used by large companies such as PayPal, Uber, Netflix and Walmart to build a strong, robust and reliable backend services.

Moving forward in this article we will describe how Node.js download, node.js tutorial, how to begin with your first node.js hello world app. But first let’s begin with brief history of Node.js-

A Brief History of Node.js:

Being 12 years old, we can say that Node.js is evolution of JavaScript, which is 25 years old, and the Web which is 32 years old. It was created by Netscape, back then popular for web Servers.

Netscape introduced an environment called Netscape Livewire that create dynamic pages using server-side JavaScript. Unfortunately, Netscape Livewire couldn’t gain success and server-side JavaScript remained mere a concept. Until, Node.js made the revolution with the use of same approach. As a fun fact, Spider Monkey is the first JavaScript Engine.

Node.js Capabilities (Why you should use Node.js?):

Asynchronous and Event Driven –

Node.js is non-blocking framework. That mean code doesn’t block execution. This way a Node.js server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

Ready to use resource-

Node.js (npm package) has vast number of libraries (up to 1,000,000 and increasing) and open-source packages you can freely use. You can jump start your work and on the way you can pick readily available libraries and functions required for your project.

Very Fast −

Being asynchronous and built on Google Chrome’s V8 JavaScript Engine, Node.js is very fast in code execution.

Advantage of Caching −

Node.js provides the caching of a single module. Whenever there is any request for the first module, it gets cached in the application memory, so you don’t need to re-execute the code.

Single-threaded −

Like JavaScript, Node.js is single threaded. A Node.js app runs in a single process, without creating a new thread for every request. For ex :- When Node.js performs an I/O operation, like reading from the network, accessing a database or the filesystem, instead of blocking the thread and wasting CPU cycles, Node.js resumes the operations when the response comes back.

Due to above feature Node.js can handle thousands of concurrent connections with a single server without the burden.

Getting Started with Node.js-

As a beginner it is natural to be confused between Node.js and JavaScript, where does JavaScript end, and where Node.js begins, and vice versa. But with time it gets clear that Node.js is a framework used for backend services.

Since Node.js is similar to JavaScript is many fashions, it is recommended to have a good grasp of the main JavaScript concepts before diving into Node.js.

Required system configuration for smooth learning and developing apps using Node.js is, minimum 4 GB RAM and at least 10 GB free space in machine. You can begin with common IDE like Visual Studio Code, Brackets, Atom, Sublime Text and WebMatrix. Let’s begin with Node.js installation and initial setup.

Node.js download and install-

We will describe the Node.js installation process for Windows. It is similar for Mac as well (except way of commands in command tool).

Download the Node.js setup-

Node.js Installer for Windows can be downloaded from official website- https://nodejs.org/en/download/. Based on your system configuration, download the 32-bit setup or 64 -bit setup.

download node js

Run the installation wizard by taping Run button and then next button to move forward.

 

run installer and click next

Accept the Terms and Conditions (which is basically granting free of charge permissions to use the software without restrictions) and in the next step select the path to install the Node.js setup.

Accept the agreement and click next to select folder for Node.js setup files

Select the default components to be included in Node.js installation files. Start the installation process by taping Install button. After the process is completed, tap Finish button.

select the default component and start installation

Once Node.js is downloaded and installed to System, validate the same using command prompt version command for Node.js, that is-

C:\Users\Admin> node -v

If node.js is completely installed on your system, the command prompt will print the version.

node.js version

Updating the Local npm version-

The final step in node.js installation is to update your local npm version, the package manager that comes bundled with Node.js. 

It is recommended to regularly update the local packages your project depends on to improve your code as improvements to its dependencies are made. 

npm install npm –global // Updates the ‘CLI’ client

To test the update, run the outdated command.

Note :- You do not need to do anything to the system variables as the windows installer takes care of the system variables itself while installing through the .msi installer

Running your first Hello World application in Node.js-

Before creating an actual “Hello, World!” application using Node.js, let us see the components of a Node.js application. It will help in understanding overall architecture.

A Node.js application consists of the following three important requirements–

  • Import required modules − We use the require directive to load Node.js modules.
  • Create server − A server which will listen to client’s requests.
  • Read request and return response − The server created in earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

Step 1 – Import Required Module

We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −

var http = require(“http”);

Step 2 – Create Server

You can use http.createServer() method to create a server instance and then bind it at port 8081 using the listen method associated with the server instance.

Check the below code :-

 

 

Note :- The above code is to create an HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.

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/'); 

Note :- The above code is to create an HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.

Step 3 – Testing Request & Response

Now you can put the code used in step 1 and Step 2 together in a file called main.js and start HTTP server as shown below –

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/');
 
run node.js program

Why Node.js is good for Web Development?

js is backed by Google’s V8 engines. Compared to other web development services, it compiles the JS code into the native device and makes it run faster. Many developers choose Node.js due to its amazing support of JavaScript and cool features. 43% of Node.js developers utilize it for enterprise apps and 85% utilize it mainly for web application development.

Node.js offers real-time two-way flawless communication between the client-side and server-side. Hence, it divides the workload between the client and the service provider.

Easy sharing of the NPM (Node Package Manager). This is an important reason why a web app development company prefers choosing Node.js. It is difficult to share anything while using another framework also.

js’s every release gets maintained actively for the next eighteen months from the date of its entry to Long-Term Support.

In fact, both PayPal and Netflix saw meaningful results in performance and economically after executing Node.js. PayPal encountered a 35% reduction in average response time for web development services while Netflix find it best for easier data streaming.

Node.js also Enhances Productivity. It allows developers to write code and utilize one scripting language for both server-side and client-side. We will find more tools that it features to improve a web development project’s work rate. js can boost your app performance by 48%. US/CA organizations that have executed Node.js into their business strategy stated that it has helped them enhance developer productivity by 68%.

how Node.js impact your business

And here is the framework usage chart. Node.js is now all time favorite framework for developers over other technologies and frameworks.

Professional Developers and used framework

Final Thoughts-

That about sums it up for this introductory tutorial on Node.js and various features, benefits from the NPM ecosystem. Our purpose is to introduce Node.js and motivate reader like you to start your Node js journey. Thanks for checking it out!

===================================================

Keywords: node js documentation, features of node js, node js tutorial point, node js tutorial geeks for geeks, node js download, node js runs on client or server, node js interview questions and preparation, node js open source free and open source example,

Arjun Mahajan
Arjun is a software engineer who loves to write on latest technology, programming and app development.
https://todaystechworld.com

Leave a Reply

Your email address will not be published. Required fields are marked *