Node Js

Before we talk about the Node js lets take some introduction about the Node JS.

Node.js is a platform built on Chrome’s JavaScript run-time for easily building fast and scalable network applications. Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications.

Why Node jS………….

Before introduced the Node Js developers have to compile their Js code in browser if take as example with Chrome web browser, it has V8 engine to compile that js code ,but after some time developers want to compile their code without browser so that they create a compiler as node js to compile Java script code,now a days they can do it with out browser.

Node.js = Runtime Environment + JavaScript Library

Where we use Node Js..

  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Real-time Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

Where Not to Use Node.js?

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

In this post we are going to talk about how to setup and do some functions in your Pc with Node Js

In first we need ti ensure our pc has Node js or not to do that go to your command Prompt and type “node” if your pc has Node it will be asked to permition to do Node function without errors,

othewise you have to install the node js with using bellow offical node websits.

https://nodejs.org/en/

after install that succefuly you can do node functions in your cmd.

Then Lets Create Out first Node Application

Before creating an actual “Hello, World!” application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components −

  • Import required modules − We use the require directive to load Node.js modules.
  • Create server − A server which will listen to client’s requests similar to Apache HTTP Server.
  • 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.

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 −

let 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 listen method 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 −

let 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/

Congratulations Your First node Js appliation is working well.You can use your favourite IDE to create that application.

Conclution..

using this post we try to get an idea about how to setup Node Js for your PC if it is not available and how to make a node js application by sending a HTTP request to the server and how to get the response from the server.

it a begging of the Node js and please try another practice about Node js and always try to compare and learn why lots of people use that one .so you will get real idea about node js.

Thank You

Leave a comment