Node.js was created by Ryan Dahl starting in 2009, and its growth is sponsored by Joyent, his employer.

Introduction Node.js

What is node.js ?
-
Evented I/O for JavaScript
-
Server Side JavaScript
-
Runs on Google's V8 JavaScript Engine
- Node single thread server
V8 js engine
Why Use Node.js ?
Node's goal is to provide an easy way to build scalable network programs.
V8 js engine
It's Fast
Tooling
npm is the Node.js package manager and it... is... excellent. It does, of course, resemble package managers from other ecosystems, but npm is fast, robust, and consistent.
Non Blocking i/o
Event Driven
It's runs javscript so you can use the same language on server and client.
Unique about Node.js
JavaScript used in client-side but node.js puts the JavaScript on server-side thus making communication between client and server will happen in same language
V8 js engine
Servers are normally thread based but Node.JS is “Event” based. Node.JS serves each request in a Evented loop that can able to handle simultaneous requests.
Node.JS programs are executed by V8 Javascript engine the same used by Google chrome browser.
How does it differ?
"Node is similar in design to and influenced by systems like Ruby's event machine or Python's twisted. Node takes the event model a bit further—it presents the event loop as a language construct instead of as a library."
You do with Node
- It is a command line tool. You download a tarball, compile and install the source.
- It lets you Layered on top of the TCP library is a HTTP and HTTPS client/server.
- The JS executed by the V8 javascript engine (the ting that makes Google Chrome so fast)
- Node provides a JavaScript API to access the network and file system.
Node is a platform for writing JavaScript applications outside web browsers. This is not the JavaScript we are familiar with in web browsers. There is no DOM built into Node, nor any other browser capability.
Node can’t run on GUI, but run on terminal
You do with Node
Node.js use event-based
In a normal process cycle the webserver while processing the request will have to wait for the IO operations and thus blocking the next request to be processed.
Node.JS process each request as events, The server doesn’t wait for the IO operation to complete while it can handle other request at the same time.
When the IO operation of first request is completed it will call-back the server to complete the request.
System Requirements
- Node runs best on the POSIX-like operating systems. These are the various UNIX derivatives (Solaris, and so on) or work a likes (Linux, Mac OS X, and so on).
- While Windows is not POSIX compatible, Node can be built on it either using POSIX compatibility environments (in Node 0.4x and earlier).
Node.js VS Apache
- It's fast
- It can handle tons of concurrent requests
- It's written in JavaScript (which means you can use the same code server side and client side)
Dissadvantage
- Node.js faster than apache but it more hungry system’s CPU and memory.
- Node.js use event based programming, it make the server doesn’t wait for the IO operation to complete while it can handle other request at the same time.
instaltion and Run progarm
Go to link and just install https://nodejs.org/
How to run program
just open cmd and got to directory path and just run command "node filename.js"
What is npm?
Npm is also the official package manager for node.js, and provides a command line interface (CLI) for interacting with the registry. This utility comes bundled with node.js and is installed automatically. For API documentation, visit https://npmjs.org/doc/ or just typenpm in your terminal.
Npm is a package maneger for javascript.
https://docs.npmjs.com/developers
How to install npm and and basic command
npm install -g <npm-name>
-g = global install
Every package not be install globally.
Configure npm
npm set init.author.name "xyz" npm set init.author.email "zyz@gmail.com" npm set init.author.url "http://xyz.com"
https://quickleft.com/blog/creating-and-publishing-a-node-js-module/
Package.json and unit test frame work
package.json - All the meta information about the project.
npm init
npm install underscor --save
Unit test Framework
- Mocha : http://code.tutsplus.com/tutorials/testing-in-nodejs--net-35018
- jasmine :http://blog.codeship.com/jasmine-node-js-application-testing-tutorial/
- unitjs,
Function and varble foramt
- %s - String.
- %d - Number (both integer and float).
- %j - JSON.
- % - single percent sign ('%'). This does not consume an argument.
util.isRegExp(object)#
util.format(format[, ...])#
Returns a formatted string using the first argument as a printf-like format.
util.format('%s:%s', 'foo'); // 'foo:%s'
var util = require('util');
util.isRegExp(/some regexp/)
// true
util.isRegExp(new RegExp('another regexp'))
// true
util.isRegExp({})
// falseutil.isDate(object)#
var util = require('util');
util.isDate(new Date())
// true
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})util.isDate(object)#
var util = require('util');
util.isDate(new Date())
// true
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})Understanding module.exports
What is a Module
A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file.
exports.sayHelloInEnglish = function() {
return "HELLO";
};
exports.sayHelloInSpanish = function() {
return "Hola";
};module.exports = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};OR
Exporting a Module
we can follow a three-step process:
var exports = module.exports = {}; var greetings = require("./greetings.js");
var greetings = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};module.exports = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};OR
Importing a Module
his process can be described in three steps:
var require = function(path) {
// ...
return module.exports;
};http://www.sitepoint.com/understanding-module-exports-exports-node-js/
//alert() is not working
console.log("log");// show the massage
console.warn("log");// show the warning
console.error("log");//show the error
Basic example
var path = require('path');
console.log(path.normalize('/a/.///b/d/../c/'));//
console.log(path.join('/a/.', './//b/', 'd/../c/'));//
console.log(path.normalize('/a/.///b/d/../c/'));//
var url = '/index.html';
console.log(path.join(process.cwd(), 'static', url));//'/home/nico/static/index.html'Use of path defulat npm
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200);
response.write("hello, this is dog");
response.end();
}).listen(5050);
console.log("listing on part 5050");Use of http defulat npm
var students = require('./students');
console.log(students.students);How to acess other file containt
exports.students = ['ashwin', 'pavan', 'vijay', 'gore'];Create appStu.js nad students.js
Routing Example
var url = require("url");
var http = require("http");
http.createServer(function(req, res){
var pathName = url.parse(req.url).pathname;
if(pathName === '/'){
res.writeHead(200, {"content-Type": "text/plain"});
res.end("home page");
}else if(pathName === '/about'){
res.writeHead(200, {"content-Type": "text/plain"});
res.end("about page");
}else if(pathName === '/pavan'){
res.writeHead(200, {"content-Type": "text/plain"});
res.end("pavan page");
}else{
res.writeHead(301, {"location":"/"});
res.end("page not found");
}
}).listen(5050);
console.log("server is runing 5050");Day02- Node JS 1.3 Installation & Setup
By Tarun Sharma
Day02- Node JS 1.3 Installation & Setup
- 899