In Focus

Creating Your First Application With Node.js

Creating your first application with Node.js An example of a web server written with Node.js.

Node.js is a very powerful JavaScript-based framework on Google Chrome's JavaScript V8 Engine. It is used to develop I/O intensive web applications like video streaming sites, single-page applications, and other web applications.

Node.js is open source, server-side platform that is completely free, and used by thousands of developers around the world.

Node.js = Runtime Environment + JavaScript Library

Features of Node.js

  1. Asynchronous and Event Driven
    All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data.
  2. No Buffering.
  3. Very Fast.

Netflix,Linkedin,Walmart,Trello,Uber,PayPal,eBay,NASA Using application written in Node.js

Prerequisites

Before proceeding you should have a basic understanding of JavaScript.

SETUP

Download Node.js from https://nodejs.org/en/download/ based on your operating system.

 
 
 
 
 
 

To verify Node.js is installed on your machine open command prompt and type - node –v

To check npm - npm –v

 

Write our First application in Node js.

Code
Use visual studio code environment or notepad++

  1. var http = require('http');  
  2. var server = http.createServer(function(req, res) {  
  3.     res.writeHead(200);  
  4.     res.end('Welcome to Node js Application Tutorial !');  
  5. });  
  6. server.listen(1234);  

 

All this code corresponds to a call to the createServer(). Its settings contain the function to be run when a visitor connects to our website.

To execute follow the below steps:

npm install http

 

node app.js

(app.js is file name)

 

Code Explanation

var http = require('http');

Makes a call to a Node.js library, here it’s the "http" library which allows us to create a web server. This can be downloaded using NPM, Node.js’s packet manager

var server = http.createServer();

We call the createSever() function contained within the http object and we save this server in the server variable. You’ll notice that the createServer function takes on a setting… and that this setting is a function.

To view output,

http://localhost:1234/