Home >>Nodejs Tutorial >Node.js First Example

Node.js First Example

Node.js First Example

Node.js applications can be console-based and web-based.

Node.js console-based Example

Example:

console.log ('Hello phptpoint');    

you can run the codeby open Node.js command prompt in your system:

ex1

Here, console.log () function displays message on console.

Node.js web-based Example

A web server node.js has contains three sections:

  1. Import required modules: To load a Node.js module, use the"require"command.
  2. Create server: You will set up a server that will listen to client requests similar to Apache HTTP Server requests.
  3. Read request and return response: The server generated in the earlier step will read the client's HTTP request that can be a browser or console, and return the response.

How to create node.js web applications

Follow these steps:

  1. Import module required:use ‘require’ Directive to load and store the HTTP moduleand returned in http instance into an http variable.
  2. var http = require("http");
  3. Build server: you can create node.js web applications by building server You must use the created http server and call http.createServer() method after that in the step twoyou can build server instance by connect it to port 8081 using the server instance-related listen method. Pass a request and answer parameter function, and write the sample implementation to return "Hello World."
For Example:

http.createServer(function (request, response) {  
// Send the HTTP header   
// HTTP Status: 200 : OK  
// Content Type: text/plain  
response.writeHead(200, {'Content-Type': 'text/plain'});  
// you can 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/');  

  • Testing Request & Response:you can start our http server and Join step1 and step2 together in a file named "main.js".
  • File: main.js

    
    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'});  
    // sending “hello world” as the response body 
    response.end('Hello World\n');  
    }).listen(8081);  
    // Console will print the message  
    console.log ('Server running at http://127.0.0.1:8081/');
    
    

    How to start your server:

    You can go to the Start menu and Click the Node.js command prompt

    ex2

    Now command prompt is open:

    ex3

    Path setting: here, we have saved the " main.js" file to the desktop here.

    So, you can type the cd desktop on the prompt for the instruction.

    Before that you can run the main.js file to get the server started as follows:

    node main.js

    ex4

    Now server is started.

    Make a request to Node.js server:

    Open http://127.0.0.1:8081/ in any browser. You will see the following result.

    ex5

    However, you will run the " main.js node" command once again, if you make any changes to the " main.js" file