Wednesday 7 December 2016

Node.js - First Application


Node.js - First Application


A Node.js application consists of the following three important components
  1. Import required modules − We use the require directive to load Node.js modules.
  2. Create server − A server which will listen to client's requests similar to Apache HTTP Server.
  3. 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 : 

Initially install http server using command as npm install http

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 3000 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(3000);

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

Step 3 - Testing Request & Response 
Let's put step 1 and 2 together in a file called app.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(3000);

// Console will print the message
console.log('Server running at http://127.0.0.1:3000/');
Now execute the app.js to start the server as follows −

$ node app.js (or) node app.js

Verify the Output. Server has started.

Server running at http://127.0.0.1:3000/. Open the browser and past the link.





0 comments:

Post a Comment