a blog for those who code

Tuesday 1 March 2016

Getting Started with Node.js

In this post we will be discussing about basics of Node.js. It is an open source, cross-platform runtime environment for developing server-side web applications. The main idea of Node.js is to use non-blocking, event-driven I/O to remain lightweight and efficient to build real-time applications which will run across distributed devices.


PC: https://nodejs.org/en/

Before going into the detail of Node.js, you might want to know why you should use Node.js. There are number of reasons why you should use Node.js. One such reason would be same programming culture on client and server will help you to resuse models, templates and reduce the size of your application. Secondly of its vibrant community with more than 50000 packages on npm to reduce your work load. One more reason of using Node.js is its ability to create real time apps through the use of websocket protocol.

Node.js uses v8 javascript engine which is the same engine used by Chrome browser. It is event driven and has non-blocking standard libraries which means if you are doing any I/O bound operations, it will happen asynchronously. Node.js is not a framework neither it is a programming language, it is just a run time environment for developing server-side web applications. You can use Node.js for creating I/O bound applications, data streaming applications, single page applications etc, but it is not advisable to use Node.js for CPU intensive applications.

When we are discussing about Node.js Basics then you should be familiar about NPM. NPM is a node package manager which handles and resolves dependencies. It makes easy for JavaScript developers to share and resuse code and it makes it easy to update the code that you're sharing. It is been called as an online repositories for Node.js packages/modules. To install modules using npm you need to run below command npm install <Module Name>.

Node.js is very easy to install. Installers can be downloaded from Node.js Download Page. In order to structure your program into different files, Node.js provides you with simple module system. To use this module system you need to use require() which is used to imports the contents from another JavaScript file.

Hello World in Node.js

var http = require('http');
var requestListener = function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}

var server = http.createServer(requestListener);
server.listen(8080); // The port where you want to start with.

Node processes your request but it doesn't wait for the back-end process to complete before moving onto the next request. So when one request is processed  Node attaches a callback function to the request, so whenever the request is ready to be responded, an event is called which trigger the associated callback function to send the response.

Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.

No comments:

Post a Comment