Node.js was created by Ryan Dahl starting in 2009, and its growth is sponsored by Joyent, his employer.
V8 js engine
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.
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.
"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."
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
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.
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"
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
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 - All the meta information about the project.
npm init
npm install underscor --save
Unit test Framework
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({})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
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'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");var students = require('./students');
console.log(students.students);exports.students = ['ashwin', 'pavan', 'vijay', 'gore'];Create appStu.js nad students.js
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");