Node.js Basics

Introduction To Node.Js

1. What is node.js?

1. Node.js is a cross platform, open source, server environment, that help run JavaScript outside the web browser.
2. Node.js can do everything that other backend languages can do.
3. It is very memory efficient in comparison to other options available because it executes a single threaded, non blocking, asynchronous programming.
4. Basically it is a JavaScript runtime that is built on top of Chrome's V8 JS engine.

2. Why Node.js?

It is very hard to deny the popularity and universality of JavaScript in modern web development. While it has great frameworks and libraries for creating interactive interfaces but it never did deploy any backend platform that could compete with other established languages. However Node.js has been the cool dude on the JavaScript block that offers very good alternative for backend.

Node.js uses event driven I/O model which makes it very efficient.

Some advantages of using Node.js:

1. JavaScript can be used for both server side and client side using Node.js.
2. Increases the effifiency of the development process as it decreases the gap between the frontend and backend developers.
3. Node.js executes code faster than any other languages.
4. Node.js provides NPM(Node Package Manager) which gives developers multiple tools and modules to use.

3. NPM

NPM is the acronym for Node Package Manager which is a package manager for the JavaScript.

1. NPM is an online repository for publishing open source node.js projects, and also it a command line utility for interacting with the online repository that helps in package installation, version management and dependency management.
2. NPM is no complex to use, you just have to run simple command line commands as:

npm install (package-name)

And that modeule will get installed in the current directory under ./node_modules/.

4. package.json file

1. package.json file holds various metadata relevant to the project and gives information to npm that allows it to identify the project as well as handle the project's dependencies.
2. It also contain project description, versions, license information, configuration data, which are very important to both npm and end users.

package.json file can be found at the root directory of a Node.js project. Lets see how the package.json file look like:


In the above example, there are fields for the description and keywords of the projects. This allows people to understand what it is in just a few words. The author, contributors, homepage and repository fields can be used to credit the people who contributed to the project.

5. Semantic versioning

Semantic versioning is the versioning style to use meaningful version numbers. It is the universal way of versioning the software development. Semantic versioning is the three component number in the format of X.Y.Z where;
X is major version.
Y is minor version.
Z is patch or revision.
So, it is the form of Major.Minor.Patch

We should change the values of X, Y and Z in following scenarios;
1. Change the value of X when existing API is broke and changed to new.
2. Change the value of Y when implementing new features in a backward-compatible way.
3. Change the value of Z when fixing bugs.

5. package-lock.json file

This file is generated automatically when the node_modules tree or package.json file are modified by npm.
1. It describes a single representation of a dependency tree such that other developers,deployments are insured to install the exact same dependencies.
2. It facilitate greater visibility of tree changes through readable source control.
3. It optimizes the installation process allowing npm to skip repeated processing of previously installed packages.
package-lock.json file look like;