Skip to main content

Simple Example of Nodejs- My First Nodejs Programm

In this section, our plan is to lead you into the world of Nodejs programming by taking you through the two basic steps required to get a simple program running. As with any application, you need to be sure that Nodejs[1] is properly installed on your computer. You also need an editor (I will suggest sublime[2] editor) and a terminal application. 

Programming in Nodejs.

 We break the process of programming in Nodejs into two steps:
  1.     Create the program by typing it into a text editor and saving it to a file named, say, app.js.
  2.     Run (or execute) it by typing "node app.js" in the terminal window.
Creating a Nodejs program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. app.js is an example program. Type these character into your text editor and save it into a file named app.js. Create a resource.json along with app.js. 

//app.js
var fs = require('fs');
fs.readFile('./resource.json', function (error, data) {
    if(error){
        console.log("erro while reading resource.json file"+error);
    }
    console.log(data);
});
//resource.json
{
    "additionalFeatures": "Contour Display, Near Field Communications (NFC), Three-axis gyroscope, " +
        "Anti-fingerprint display coating, Internet Calling support (VoIP/SIP)",
    "android": {
        "os": "Android 2.3",
        "ui": "Android"
    },
    "availability": [
        "M1,",
        "O2,",
        "Orange,",
        "Singtel,",
        "StarHub,",
        "T-Mobile,",
        "Vodafone"
    ],
    "battery": {
        "standbyTime": "428 hours",
        "talkTime": "6 hours",
        "type": "Lithium Ion (Li-Ion) (1500 mAH)"
    }
}

Executing a Nodejs program. This is the exciting part, where the node follows your instructions.
To run the file reading program, type the following at the terminal: 


$ node app.js
 
If all goes well, you should see the following response 
 {
    "additionalFeatures": "Contour Display, Near Field Communications (NFC), Three-axis gyroscope, " +
        "Anti-fingerprint display coating, Internet Calling support (VoIP/SIP)",
    "android": {
        "os": "Android 2.3",
        "ui": "Android"
    },
    "availability": [
        "M1,",
        "O2,",
        "Orange,",
        "Singtel,",
        "StarHub,",
        "T-Mobile,",
        "Vodafone"
    ],
    "battery": {
        "standbyTime": "428 hours",
        "talkTime": "6 hours",
        "type": "Lithium Ion (Li-Ion) (1500 mAH)"
    }
}


Understanding NodeJs Program:

In this program, we read the resource.json file from disk. When all the data is read, anonymous function is called (a.k.a. the “callback”) containing the arguments error, if any error occurred, and data , which is the file data.

Comments

Popular posts from this blog

Introduction to Node.js - Part-1 Node Command line Interface(CLI) App

Aim This document is get to start with the Node.Js. This is a simple introduction to Node and Its corresponding frameworks. This will help you to create a simple chit chat server application using node.js, express.js and socket.io. I have used the chat demo app of socket.io to learn you node. Introduction to Node.Js CLI Introduction to Node If you are from the Java and .net background. You might have create several JSP and ASP application to learn Java and ASP web framework. While create web app. You also learned JavaScript to interact with the HTML dom elements. If you go to the wikipedia definition of node which is “ Node.js is an open source, cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, and IBM_i ”. It tells that Node is a framework where you can write code in Javascript programming language on s

Loading- Exporting Modules in Node.Js

Loading a module: In node you can load module either using the path to the module or giving name  of the module. require keyword is used to load  module in node. Its look for the file in given path to load module if the require module is not core node module. You can install third party module using NPM (Node Package Manger). It downloads from the global repository to your local machine. var module = require( 'module-name' ); The require function returns an object that represent the module which have exported. It can be any javascript data type. It could be function, an array, any javascript object. Exporting a module: The CommonJs Module system is use to share the object or properties in node.js. Since Javaascript runs program in global scope. CommonJs Module is one of the best way for creating and sharing namespace in the javascript environment. Here below given an example to export a function in node.js. //person.js /** * Created by deepak.m.shrma@gm

Provide SSL support over your http NodeJs-Express API

A simple HTTPS server using node.js: Using ExpressJS and openssl, you can easily provide SSL support to your Api. For that you have to follow below given steps. Steps to create a https server. Generate an SSL certificate with that key Create a app.js file Install express module Run to Test your app. :-) Generate an SSL certificate with that key: For development purposes you can create a self-certified certificate. First, generate a private key on linux-based system. It will store a 1024 bit RSA key in the file key.pem openssl genrsa 1024 > key.pem Then, generate an SSL certificate with that key: openssl req -x509 -new -key key.pem > key-cert.pem Create a app.js file var fs = require("fs"),                 //Requires fs module to read key, cert files     express = require('express'),       // call express     app = express(),                     // define our app using express     http = require("http"),