SOURCE : https://www.edureka.co/blog/nodejs-tutorial/
If you have ever heard about Node.js then you might know that it is one the most prominent and powerful frameworks of JavaScript. Since its release, it has continued to keep its stranglehold on the IT market. Even with the introduction of new and vibrant JS Frameworks like Angular, React, Meteor etc., the popularity of Node.js never seems to cease. Wondering why? Get ready to fall in love with Node.js.
What is Node.js?
Node.js is a powerful framework developed on Chrome’s V8 JavaScript engine thatcompiles the JavaScript directly into the native machine code. It is a lightweight framework used for creating server-side web applications and extends JavaScript API to offer usual server-side functionalities. It is generally used for large-scale application development, especially for video streaming sites, single page application, and other web applications.
Node.js makes use of an event-driven, non-blocking we/O model which makes it a right pick for the data-intensive real-time applications.
Like any other programming languages, node.js makes use of packages and modules.
These are the libraries that contain various functions and are imported from npm (node package manager) into our code and utilized in the programs. Some of the major features that define Node.js are listed below:
Features of Node.js
- Open Source
Node.js is an open source framework MIT license that is supported by a huge community. Its community is pretty much active have contributed to add new capabilities to Node.js applications.
- Simple and Fast
Since Node.js is built on Google Chrome’s V8 JavaScript Engine, its libraries are capable of fast code execution.
- Asynchronous
All the libraries of Node.js are asynchronous which means the Node.js based servers never wait for an API to send back the response and move on to the next API.
- High Scalability
Because of the event mechanism, Node.js is highly scalable and aids the server in a non-blocking response.
- Single-Threaded
With the help of event looping, Node.js is able to follow the single-threaded model. This lets a single program to handle multiple requests.
- No Buffering
One of the major functionalities of Node.js applications is that it never buffers any data.
- Cross-Platform
Node.js can be easily built and deployed on various platforms like Windows, MAC, and Linux.
NPM (Node Package Manager)
NPM stands for Node Package Manager which as the name suggests is a package manager for Node.js packages/modules. From Node version 0.6.0. onwards, npm has been added as default in the node installation. It saves you from the hassle of installing npm explicitly.
NPM basically helps in two ways:
- Provides and hosts Online repositories for node.js packages/modules which can be easily downloaded and used in our projects. You can find them here: npmjs.com
- Provides the Command line utility in order to install various Node.js packages, manage Node.js versions and dependencies of the packages.
But now, you must be wondering what exactly these modules are and how do they help us in building the Node.js applications. Well, in the next section of this Node.js tutorial, we will give you a complete insight into Node.js modules.
Node.js Modules
The modules in Node.js represents various functionalities that are bundled up into single or multiple JS files. These modules have a unique context, thus, they never interfere nor pollute the scope of other modules.
These modules enable the code reusability and enhance the ease of usage. Node.js basically provides three types of modules:
- Core Modules
- Local Modules
- Third-Party Modules
Core Module
Since Node.js is a very lightweight framework, the core modules bundle the absolute minimum functionalities. These modules generally get loaded when the Node process starts its execution. All you need to do is, import these core modules in order to use them in your code.
Below we have listed down a few of the important Core modules.
Core Module | Description |
http | Contains classes, methods, and events required to create Node.js HTTP server |
url | Contains methods for URL resolution and parsing in Node |
querystring | Contains methods to deal with a query string of Node |
path | Contains methods to deal with file paths |
fs | Contains classes, methods, and events to work with file we/O |
util | Contains utility functions that can be useful for programmers |
You can load your core module, using the below code:
1 | var module = require(‘module_name’); |
Lets now see, what are ‘local modules’.
Local Modules
The local modules of Node.js are custom modules that are created locally by user/developer in the application. These modules can include various functionalities bundled into distinct files and folders which can be easily distributed in the Node.js community using NPM.
These modules are loaded in a similar way to core modules. Let show you, how to do it using a basic example.
Create your custom/local module.js file
1
2 3 4 5 6 7 8 9 10 |
var detail = {
name: function (name) { console.log(‘Name: ‘ + name); }, domain:function (domain) { console.log(‘Domain: ‘ + domain); } };
module.exports = detail; |
Include your module file in your main application file.
These modules can include various functionalities bundled into distinct files and folders which can be easily distributed in the Node.js community using NPM.
These modules are loaded in a similar way to core modules. Let show you, how to do it using a basic example.
Create your custom/local module.js file
1
2 3 4 5 6 7 8 9 10 |
var detail = {
name: function (name) { console.log(‘Name: ‘ + name); }, domain:function (domain) { console.log(‘Domain: ‘ + domain); } };
module.exports = detail; |
Include your module file in your main application file.
1
2 3 |
var myLogModule = require(‘./Local_module.js’);
myLogModule.name(‘Edureka’); myLogModule.domain(‘Education’); |
Now you can execute these files using below command:
1 | node application.js |
Let me now show you what are external modules.
External Modules
You can use the external or 3rd party modules only by downloading them via NPM. These modules are generally developed by other developers and are free to use. Few of the best external modules are express, react, gulp, mongoose, mocha etc.
Globally Loading the 3rd party modules:
1 | npm install –g <module_name> |
Include your module file in your main application file:
1 | npm install –save <module_name> |
JSON File
The package.json file in Node.js is the heart of the entire application. It is basically the manifest file that contains the metadata of the project. Thus, understanding and working with this file becomes very important for a successful Node project development.
The package.json file generally consists of the metadata of the application, which is further categorized into below two categories:
- Identifying metadata properties: This consists of properties like the project name, current module version, license, author of the project, project description etc.
- Writing directly to file: You can directly write the necessary information into the package.json file and include it, in your project.
By now you have already familiarized with the various components of Node JS application. In the next section of this Node.js Tutorial, we will be sharing some Node Js basics so that we can start off with the hands on.
For Node.js Basics, Data Types and much more on this, stay tuned till next week!