Skip to main content

A brief introduction on Node js

When I hear about Node js, there were a lot of questions in my mind. There was a saying "You can do whatever you want, Javascript provides you". Back then, I was thinking whether I could do backend applications using Javascript also. Because when I started my career, there were front-end developers who purely work on HTML, CSS, Javascript and some of JS libraries. And back-end developers main concentration on Java, Spring, JSP etc.

        That time, I was asking myself that, is it really necessary to have two separate things when it comes to application development? But then Node js came into the picture.

You may think, "What is Node js?".

        Node js is an open source server-side framework which allows you to run javascript on the server. It runs on various platforms like Windows, Linux, Unix, Mac OS X, etc., It is built on Chrome's V8 Javascript Engine. A key point is to be noted here is Node js compiles in Runtime execution of the application, which makes the application run in less time.

       So basically if I want to tell about Node js to Java developers, the following comparison is more than enough.

                                    Node                 →     JRE
                                    Javascript          →     Java
                                    v8 js engine       →     JDK
                                    npm                   →     maven / gradle
                                    package.json     →    pom.xml

Normally there are 3 layers when developing an application. They are as follows:
1. Presentation Layer
2. Business Layer
3. Data Layer

Here, the Business layer is subdivided into:
1. Client-side
2. Server-side

You can use Node js for both client-side and server-side programming since it uses Javascript.

Before you proceed further, you need to know "What is npm?".

npm is a package manager for Node js packages or modules. If you want to install/use any packages/modules in the application, you can just install it under your project folder and use it in your application. npm is having the world's highest downloads till now.

You can just run following command to install any package:
Ex: Say you want to download and use jquery module, then you can just use the following command
$ npm install jquery

Once Installation completes, your module will be saved inside a folder called node_modules.

"What are the ways to run Node?"

Actually, there are two ways based on the application.
1. If you need to run any node js related .js files[i.e., script file] then the command is
$ node filename.js

2. If you have a complete folder structure and want to run as an application, then the command is
$ npm start

If you want to start with node js, then package.json file is important. You might think, "What does package.json file contains?"

The package.json is the first entry point for any node applications. It contains following structure.

{
  "name": "webnode",
  "description": "Description related to the application",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "ejs": "~2.5.7",
    "express": "~4.15.5",
    "morgan": "~1.9.0",
    "serve-favicon": "~2.4.5"
  },
  "devdependencies": {
 "eslint": "~1.0.0",
 "babel-core": "~1.0.0"
  }
}
The command to run any Node application starts with what you mention inside "scripts" object inside the package.json file. Here according to the example shown above, my command for running application will be

$ npm start

Basically, it is running ./bin/www file, where all the configuration of the server will be made.

How to generate package.json file?

Step 1: Create a folder and name it.
Step 2: Then do cd ./folderName to enter into folder.
Step 3: Run the command "npm init".

This will create the package.json file inside your working folder.


In my upcoming blogs, I'll be narrating you how to create applications using Node.js. Thank you for your valuable time.


Comments

Post a Comment

Popular posts from this blog

Creating A Simple Application Using Node js.

In my last blog, I gave a gist of information about Node js. In this blog, I'll be explaining you about creating a simple application using Node js. To check which version of Node installed on your machine, go to cmd terminal, and run $ node --version And to check the version of npm, run $ npm --version I am now using node: v6.9.5 and npm: v5.6.0 in my system. Before proceeding there is something you need to know. i.e., Modules So let me ask you " What are modules? " Modules are set of functions/methods, which is very much useful for any application to build. Normally Node js itself has its own built-in modules, which you can directly use in your application without installing them. Normally you can use modules in three different ways, 1. Using Node js built-in modules. 2. Writing your own modules and then use. 3. Installing external modules using npm and then use. I'll list some of the commonly used built-in modules of Node js version 6.10.3, whic...