banner



How To Create Node Js

Author avatar

Getting Started with NodeJS

Gaurav Bhavsar

Introduction

In this article, I am going to introduce NodeJS with Node Package Module (NPM), step-by-step basic implementation and explanation.

This article covers the following areas of NodeJS.

  • Installation of NodeJS and NPM
  • Node Package Module (NPM)

NodeJS

NodeJS is an open-source, cross-platform runtime environment for developing server-side web applications. NodeJS also has an event-driven architecture capable of asynchronous I/O.

NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Installation of NodeJS and NPM

Installation of NodeJS and NPM is straightforward using the installer package available at NodeJS official web site.

  • Follow the installer steps, agree the license agreement and click the next button.
  • Restart your system/machine.

Now, test NodeJS by printing its version using the following command in Command Prompt:

and test npm by printing its version using command

Simple way to test nodeJS work in your system is to create a javascript file which print a message.

                                      1                    /*test.js file*/                                                            2                                        console                    .                    log                    (                    "Node is working"                    )                    ;                                  

js

Run the test.js file using Node command > node test.js in command prompt.

description

You are done with installation.

Node Package Module

NPM is the package module which helps javascript developers load dependencies effectively. To load dependencies we just have to run a command in command prompt:

This command is finding a json file named as package.json in root directory to install all dependencies defined in the file.

Package.json

                                      1                    {                                                            2                                                            "name"                    :                                                            "ApplicationName"                    ,                                                            3                                                            "version"                    :                                                            "0.0.1"                    ,                                                            4                                                            "description"                    :                                                            "Application Description"                    ,                                                            5                                                            "main"                    :                                                            "server.js"                    ,                                                            6                                                            "scripts"                    :                                                            {                                                            7                                                            "start"                    :                                                            "node server.js"                                                            8                                                            }                    ,                                                            9                                                            "repository"                    :                                                            {                                                            10                                                            "type"                    :                                                            "git"                    ,                                                            11                                                            "url"                    :                                                            "https://github.com/npm/npm.git"                                                            12                                                            }                    ,                                                            13                                                            "dependencies"                    :                                                            {                                                            14                                                            "express"                    :                                                            "~3.0.1"                    ,                                                            15                                                            "sequelize"                    :                                                            "latest"                    ,                                                            16                                                            "q"                    :                                                            "latest"                    ,                                                            17                                                            "tedious"                    :                                                            "latest"                    ,                                                            18                                                            "angular"                    :                                                            "latest"                    ,                                                            19                                                            "angular-ui-router"                    :                                                            "~0.2.11"                    ,                                                            20                                                            "path"                    :                                                            "latest"                    ,                                                            21                                                            "dat-gui"                    :                                                            "latest"                                                            22                                                            }                                                            23                                        }                                  

json

The most important things in your package.json are name and version. Those are actually required, and your package won't install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.

Repository

                                      1                    {                                                            2                                                            "repository"                    :                                                            {                                                            3                                                            "type"                    :                                                            "git"                    ,                                                            4                                                            "url"                    :                                                            "https://github.com/npm/npm.git"                                                            5                                                            }                                                            6                                        }                                  

json

Specify the place where your code lives. Through this repository, developers can reach out and contribute to your application. If the git repository is not GitHub, then the npm docs command will be able to find you.

Scripts

                                      1                    {                                                            2                                                            "scripts"                    :                                                            {                                                            3                                                            "start"                    :                                                            "node server.js"                                                            4                                                            }                                                            5                                        }                                  

json

NPM provide many useful Scripts like npm install , npm start , npm stop etc.

Some default script values are based on package contents.

                                      1                    "start"                    :                                                            "node server.js"                                  

json

If there is a server.js file in the root of your package, then npm will default the start command to node server.js.

Dependencies

                                      1                    {                                                            2                                        "dependencies"                    :                                                            {                                                            3                                                            "express"                    :                                                            "~3.0.1"                    ,                                                            4                                                            "sequelize"                    :                    "latest"                    ,                                                            5                                                            "q"                    :                    "latest"                    ,                                                            6                                                            "tedious"                    :                    "latest"                    ,                                                            7                                                            "angular"                    :                    "latest"                    ,                                                            8                                                            "angular-ui-router"                    :                                                            "~0.2.11"                    ,                                                            9                                                            "path"                    :                    "latest"                    ,                                                            10                                                            "dat-gui"                    :                    "latest"                                                            11                                                            }                                                            12                                        }                                  

json

Dependencies are specified in a simple object that maps a package name to a version range. Version Name must be Version exactly.

If you want to install the latest version of a file, you just have to put latest in place of the version name.

Tilde(~) is used to tell "Approximately equivalent to version".

Basic Example

Create a server.js javascript file with following code

                                      1                    /*server.js*/                                                            2                    3                                        const                                          http                                        =                                                            require                    (                    'http'                    )                    ;                                                            4                    5                                        const                                          hostname                                        =                                                            '127.0.0.1'                    ;                                                            6                                        const                                          port                                        =                                                            3000                    ;                                                            7                    8                                        const                                          server                                        =                                          http                    .                    createServer                    (                    function                    (                    req                    ,                                          res                    )                                                            {                                                            9                                          res                    .                    statusCode                                                            =                                                            200                    ;                                                            10                                          res                    .                    setHeader                    (                    'Content-Type'                    ,                                                            'text/plain'                    )                    ;                                                            11                                          res                    .                    end                    (                    'Hello World\n'                    )                    ;                                                            12                                        }                    )                    ;                                                            13                    14                    server                    .                    listen                    (                    port                    ,                                          hostname                    ,                                                            function                    (                    )                                                            {                                                            15                                                            console                    .                    log                    (                    'Server running at http://'                    +                                          hostname                                        +                                                            ':'                                                            +                                          port                                        +                                                            '/'                    )                    ;                                                            16                                        }                    )                    ;                                  

js

As we need http to create an http server we use require('http') and pass it to a variable named http

                                      1                    var                                          http                                        =                                                            require                    (                    'http'                    )                    ;                                  

js

We also need to defined hostname and port number, here we use localHost i.e 127.0.0.1 and port number 3000 and assign this to the variables hostname and port , respectively.

                                      1                    var                                          hostname                                        =                                                            '127.0.0.1'                    ;                                                            2                                        var                                          port                                        =                                                            3000                    ;                                  

js

Next we create the http server using the createServer method.

                                      1                    var                                          server                                        =                                          http                    .                    createServer                    (                    function                    (                    req                    ,                                          res                    )                    {                                                            2                                          res                    .                    statusCode                                                            =                                                            200                    ;                                                            3                                          res                    .                    setHeader                    (                    'Content-Type'                    ,                                                            'text/plain'                    )                    ;                                                            4                                          res                    .                    end                    (                    'Hello World\n'                    )                    ;                                                            5                                        }                    )                    ;                                  

js

This created the server as well as a response having statusCode: 200 , Content-Type header of plain text and and ends with the string Hello World . This is the response that the server can send to browser.

the function has two parameters req and res which is the request from and response to the server, respectively.

In our example we are creating responses.

We created the server, now we have to assign it a hostname and port number.

                                      1                    server                    .                    listen                    (                    port                    ,                                          hostname                    ,                                                            function                    (                    )                                                            {                                                            2                                                            console                    .                    log                    (                    'Server running at http://'                    +                                          hostname                                        +                                                            ':'                                                            +                                          port                                        +                                                            '/'                    )                    ;                                                            3                                        }                    )                    ;                                  

js

Here, the server listens to localhost on port 3000 and prints "Server running at http://127.0.0.1:3000/" in command prompt.

Now Run server.js file un node using command

Open a browser and enter url http://127.0.0.1:3000/. The browser will display Hello World message on the screen.

description

description

How To Create Node Js

Source: https://www.pluralsight.com/guides/getting-started-with-nodejs

Posted by: williamswasioneating.blogspot.com

0 Response to "How To Create Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel