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@g...

Simple HTTP server programm in NodeJs

Most common use of Node is to create a servers. Node gives you very simple way to create different type of servers. Here using a Node i will create a simple HTTP server which will listen a port and response simple text message to every client which try to connect this HTTP server. Create a httpserver.js: var http = require('http'); var server = http.createServer(); server.on('request', function (req, res) {     res.writeHead(200, {'Content-Type': 'text/plain'});     res.end('Hello NodeJs Ninja\n'); }); server.listen(3000); console.log('Server running at http://localhost:3000/'); Explanation: Here what we have did. we have created a simple HTTP server which is listening port 3000 of your local server. Whenever a request happens, the anonymous [ 3 ] function (req, res) callback is fired and “Hello NodeJs Ninja” is written out as the response. You can compare it with the onclick event of the browser. Whenever user click on some ele...