When we say about the Node.js there can be two type of application that are:-
(1)Console Based Node.js Application
(2)Web based Node.js Application
Node.js console based example
File: console_example.js
console.log('Hello CrackYourInterview')
Now you need to open Node.js command prompt and run the following code:
node console_example.js
Here, in above screen console.log()function displays message on console.
Node.js Web Based example
A node.js web application contains the below three parts:-
(1)Import required modules:-The "require" directive is mainly used to load a Node.js module.
(2)Create Server:-Here you need to establish a server which will listen a client request which is same as we do with Apache HTTP Server.
(3)Read request and return response:-We have create a server in second step will read HTTP request made by client which can either be a browser or console and will return the response.
Below is the example to create node.js web applications
Follow below steps with syntax:-
(1)Import Required Module:-In first step is use ?require? directive to load http module and store returned HTTP instance into http variable and belos is the example:-
var http = require("http");
(2)Create Server:-In this second step, you have to use created http instance and then need to call http.createServer() method to create server instance. And after that you will bind it at port 8081 by using listen method associated with server instance. Pass it a function with request and response parameters 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'});
// 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/');
(3)Combine step1 and step2 together:-In this step we will put file name "main.js".
File: main.js
var http = require("http");
http.createServer(function (request, response) {
// Here 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 step how to start your server:-
No you need to go to start menu and click on Node.js command prompt.
now open command prompt:-
Set path: Here you need to save "main.js" file on the desktop.
So first you need to go to "desktop" folder where we have save the file "main.js" in that folder. And once you execute the main.js to start the server as follows:-
node main.js
Now server is started
Now you need to make a request to Node.js server:-
One you open http://127.0.0.1:8081/ in any browser. You will get the below result.
Everytime, it you make any changes in file "main.js" file, you need to again run the "node main.js" command.