Saravanan's Corner: Blackberry Dev

Thursday, 20 September 2018

Node JS



Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). 

- Node.js is an open source, cross-platform runtime environment for developing 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, and Linux.

Download Node.js archive
Download latest version of Node.js installable archive file from Node.js Download

Installation on Windows

Use the MSI file and follow the prompts to install the Node.js. By default, the installer uses the Node.js distribution in C:\Program Files\nodejs. The installer should set the C:\Program Files\nodejs\bin directory in window's PATH environment variable. Restart any open command prompts for the change to take effect.

Verify installation: Executing a File

Create a js file named main.js on your machine (Windows or Linux) having the following code.
/* Hello, World! program in node.js */
console.log("Hello, World!")
Now execute main.js file using Node.js interpreter to see the result:
$ node main.js
If everything is fine with your installation, this should produce the following result:
Hello, World!

Creating Node.js Application

Step 1 - Import Required Module

We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −
var http = require("http");

Step 2 - Create Server

We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listenmethod associated with the server instance. Pass it a function with parameters request and response. Write the sample implementation to always return "Hello World".
http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
The above code is enough to create an HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.

Step 3 - Testing Request & Response

Let's put step 1 and 2 together in a file called main.js and start our HTTP server as shown below −
var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Now execute the main.js to start the server as follows −
$ node main.js
Verify the Output. Server has started.
Server running at http://127.0.0.1:8081/

Make a Request to the Node.js Server

Open http://127.0.0.1:8081/ in any browser and observe the following result.
Node.js Sample
Congratulations, you have your first HTTP server up and running which is responding to all the HTTP requests at port 8081.




response.render -> eJs will compile and send to html...

const _ = require('lodash');
var validator = require('validator');
var user = require('os');
const note = require('./notes.js');
var option = process.argv[2];


if(option === 'list'){
console.log('your option is list all notes');
console.log(note.addnote());
} else if (option === 'read'){
console.log('your option is read note');
} else if (option === 'remove'){
console.log('your option is remove a note');
} else {
console.log('Invalid Option');
}

var i = 0;

do{
console.log('value' + i++);
}while(i<=10);
























No comments:

Post a Comment