NodeJS

NODEJS  https://www.tutorialspoint.com/nodejs/nodejs_first_application.htm



Node.js is a platform built on Chrome's JavaScript runtime(V8 Engine) for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.



Node.js = Runtime Environment + JavaScript Library

It is not advisable to use Node.js for CPU intensive applications.


Features of Node.js



Asynchronous and Event Driven − All APIs of Node.js library are asynchronous.


Single Threaded but Highly Scalable − Node.js uses a single threaded model with event looping.

No Buffering − Node.js applications never buffer any data.



                                Node.js Concepts




















The source files(NotePad editor) for Node.js programs are typically named with the extension ".js".

The source code written in source file is simply javascript. The Node.js interpreter will be used to interpret and execute your javascript code.

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

Sample Example in NodeJs:

1. 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






















A Node.js application consists of the following three important components −

  • Import required modules − We use the require directive to load Node.js modules.
       ex: 
           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");

  • Create server − A server which will listen to client's requests similar to Apache HTTP Server.
  • ex:
  • 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 listen method associated with the server instance.  Pass it a function with parameters request and response.

  • Read request and return response − The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.
  • ex:
  • 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

No comments:

Post a Comment

JAVA NOTES

  click here  👉  Interview  Preparation : complete notes sql queries notes ============================================================ Con...