3. Installation:3way to install
• 1st way: Nodejs.org :
– You can download pre-compiled binary
– Easy installation
– Automatic configured environment
– But, updates require reinstallation
– And switching versions need reinstallation
4. 2nd way : Version manager(nvm):
Install any version quickly
Switch version quickly
Easy install on Linux
But no widzard based installation(must use
command line interface)
And reinstall global modules when switching version
Install version manager then node
5. 3rd way : Compile it from source(node)
Get absolute new node
Customize your installation
But you must be familiar with compiling process
And takes longer
And not easy to switch
13. Why node.js ?
• It is js with browser
• JS for both front end and backend (eg: using
jquery)
• Native support
• It’s fast because it’s mostly C code
14. What can you build?
•
•
•
•
Websocket server ( Like chat server)
Fast file upload client
Ad server
Any Real- Time Data Apps
15. What is Node.js not:
• Web framework
• For Beginners (it’s very low level)
• Multi- threaded (You can think of it as a single
threaded server)
16. Few variables you are familiar about
web browser:
• window : (Type in console of chrome)
The window object have functions and
attributes that have to do something with
window being drawn in screen.
17. • location: (Type in console of chrome)
The location object has all the information about url
that being loaded.
18. • document : (Type in console of chrome)
The document object contains all oh html displayed
in page
19. Now type in node console:
• It displays undefined message
20. Why not work in node?
• Because node is not just an instance , so
“global” is defined in node but not in chrome
console where you can get instance version of
global object called window.
21. • This global object has several functions (which
are not in chrome console)
• EG: require function: used to pull in different
javascript script files from your appliction
30. REQUIRING MODULES
var http = require('http'); //http.js
va r fs = require('fs'); // fs.js
How does ‘require’ return the libraries?
How does it find these files?
31. Custom Module Example:
• Prabin_App(base folder)
– Example.js
var say = require(‘./say_hello_module.js’); //say is assigned as object & has 2 properties :
softly and loudly which both are functions
say.softly(‘prabin’);
Say.loudly(‘PRABIN’);
– Say_hello_module(Module Folder)
• index.js
var hello = function (message)
{
console.log(‘say hello:’+message);
});
exports.slowly = hello;//we first declare hello as function and setting slowly to be value of
hello
exports.loudly = function(message)
{
console.log(‘SAY HELLO:’+message);
});
39. NPM: THE USERLAND SEA
“Core” is small. “Userland” is large.
Package manager for node
• Comes with node
• Module Repository
• Dependency Management
• Easily publish modules
• “Local Only”
40. INSTALLING A NPM MODULE
/Home/my_app
• $ npm install request
https://github.com/mikeal/request
Installs into local node_modules directory
---------------after installation------------------------------Home/my_app/node_modules/request
----For require/using in your app-----------------------------------/Home/my_app/app.js
va r request = require('request');//Loads from local
node_modules directory
41. LOCAL VS GLOBAL MODULE
• Install modules with executables globally:
$ npm install coffee-script –g //global
$ coffee app.coffee
Note: Global npm modules can’t be required
$ npm install coffee-script //Install locally
43. DEFINING YOUR DEPENDENCIES
my_app/package.json
{
"name": "My App",
"version": "1",
"dependencies":
{
"connect": "1.8.7"
}
}
Then: $ npm install will installs it into the node_modules directory
i.e. my_app/node_modules/connect
46. NODE.JS : Hello world
Hello.js
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200); //Status code in browser
response.write("Hello World.");//ResponseText
response.end();//Close the connection
}).listen(8080);//Listen for connection on this port
console.log('Listening on port 8080...');
47. • Run the server:
$ node Hello.js
(Listening on port 8080...)
• View output
$ curl http://localhost:8080
(Hello World)
50. Long Running Process
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200);
response.write(“Hello World running.");
response.end();
setTimeout(function() //Represent long running process
{
response.write(" Hello World is done.");
response.end();
}, 5000);
//5000ms = 5 seconds
}).listen(8080);
51. TWO CALLBACKS HERE
var http = require('http');
request
http.createServer(function(request, response) {
response.writeHead(200);
response.write(“Hello World running.");
response.end();
timeout
setTimeout(function() //Represent long running process
{
response.write(" Hello World is done.");
response.end();
}, 5000);
//5000ms = 5 seconds
}).listen(8080);