How to create Node.js Application?
Node.js console-based
create file test.js
console.log('Hello ittutorial.in');
Here, console.log() function displays message Hello ittutorial.in on console.
File Name : test.js
Open Node.js command prompt and run the following code:
node test.js
Node.js web-based Application example
A node.js web application contains the following three parts:
Import required modules: The "require" directive is used to load a Node.js module.
Create server: You have to establish a server which will listen to client's request similar to Apache HTTP Server.
Read request and return response: Server created in the second step will read HTTP request made by client which can be a browser or console and return the response.
How to create node.js web applications
1. Import required module: The first step is to use ?require? directive to load http module and store returned HTTP instance into http variable. For example:
var http = require("http");
2. Create server: In the second step, you have to use created http instance and call http.createServer() method to create server instance and then bind it at port 8081 using
listen method associated with server instance.
Pass it a function with request and response parameters.
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 Ittutorial"
response.end('Hello ItTutorial\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
test.js
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello ItTutorial\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
Example :-
File Name : test.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello itechxpert.in!');
}).listen(8080);
File Name :
Now execute the main.js to start the server as follows −
node test.js
Creating a Web Server using Node
Node.js provides an http module which can be used to create an HTTP client of a server.
File Name : mynode.js
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console.log("Request for " + pathname + " received.");
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
} else {
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Previous
Next