A simple application using Node.js


As nodejs.org states, Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.


Creating a Simple app using Node.js

  • To install Node.js simply head over to nodejs.org and download the version recommended for most users and run through the installer.



  • Once the installation is finished type node –v in the terminal to check whether Node.js is running successfully



  • If you get something like above, you have installed Node.js successfully.

Let’s start coding……


  • Go to your favorite IDE( here I have used webStrome) and go to New project -> Empty project and create a new folder to set project workspace  finally hit the create button.
  • Now in that newly created folder create a javaScript file called “Server”.This is the file Node.js will execute at the end. So in this file we tell Node.js to listen to incoming requests and do something upon that request.


  • In the first line http module gives access to createServer method obviously which will create a new server.
  • By chaining listen method to createServer method it tells server to listen to that particular port which has passed as a parameter. Server should handle all the requests coming through this port. To achieve that handleRequset function is defined.
  • writeHead() changes the header of  the request.write() method will render plain text to screen. end() method is used to aware that response handling is done and display output to the person who requested.
  • Now save the file and open a terminal in the folder where we saved the javaScript file.
  • Run the following command in the terminal to execute javaScript file with Node.js

  • Once it’s done go to a browser and navigate to localhost:3000




Congratulations! you have programmed a simple app using NodeJS.
Please note that this just a basic example. You can create many more advance applications using NodeJS. And if you are willing to learn more there are thousands of posts related to Node.js which you can refer in to.

Comments

Popular posts from this blog

Consuming API from web application using OAuth 2.0

Simple REST API using Node,Express and MongoDB

Create a basic web Server using express