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
- 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. - No Buffering.
- 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++
- var http = require('http');
- var server = http.createServer(function(req, res) {
- res.writeHead(200);
- res.end('Welcome to Node js Application Tutorial !');
- });
- 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/