Node.js Introduction
Node.js is an open source server environment which is free.Node.js runs on various platforms lik as Windows, Linux, Unix, Mac OS X, etc and use JavaScript on the server.This is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine) and developed by Ryan Dahl in 2009 and its latest version is v0.10.36.Node.js uses asynchronous programming.This is also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent.
Download Node.js from https://nodejs.org
Node.js = Runtime Environment + JavaScript Library
Some Features of Node.js
Some features are must important that make Node.js the first choice of software architects.
- Asynchronous and Event Driven
- Very Fast
- Single Threaded but Highly Scalable
- No Buffering
- License
Used Node js in
- I/O bound Applications
- Data Streaming Applications
- Data Intensive Real-time Applications (DIRT)
- JSON APIs based Applications
- SPA(Single Page Applications)
Getting Started
The main require directive to load the http module and store the returned HTTP instance into an http variable as give below.
Step 1.
var http = require("http");
Step 2.
We are already the created http instance and call http.createServer() method to create a server instance and then we bind it at port 1111 using the listen method associated with the server instance
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(1111);
// Console will print the message
console.log('Server running at http://27.34.69.69:1111/');
In the given above code is enough to create an HTTP server which listens, i.e., waits for a request over 1111 port on the local machine.
Step 3
Let's put step 1 and 2 together in a file called demo.js and start our HTTP server as a given 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(1111);
// Console will print the message
console.log('Server running at http://27.34.69.69:1111/');
Now execute the demo.js to start the server as given below.
$ node main.js
Comments