Complete Guide On Node.js
By:
Senior Developer
Prabin Silwal
What you should know before?
Javascript
Unix command
Installation:3way to install
• 1st way: Nodejs.org :
– You can download pre-compiled binary
– Easy installation
– Automati...
2nd way : Version manager(nvm):
Install any version quickly
Switch version quickly
Easy install on Linux
But no widzard ba...
3rd way : Compile it from source(node)
Get absolute new node
Customize your installation
But you must be familiar with com...
Which is preferable?
• For mine 2nd way
Window installation:
• Download node.js
• Click install
Checking after installation?
• Run command
• Type “node” and hit enter
• Type “console.log(‘your text’)”
Note that previous installation process
needs re installation for new updates.
Installing using nvm in Linux(Not
wizard mode for Linux):
Make ready to install by following
command:
• sudo apt-get install git
• sudo apt-get install curl
Install using command:
Why node.js ?
• It is js with browser
• JS for both front end and backend (eg: using
jquery)
• Native support
• It’s fast ...
What can you build?
•
•
•
•

Websocket server ( Like chat server)
Fast file upload client
Ad server
Any Real- Time Data Ap...
What is Node.js not:
• Web framework
• For Beginners (it’s very low level)
• Multi- threaded (You can think of it as a sin...
Few variables you are familiar about
web browser:
• window : (Type in console of chrome)
The window object have functions ...
• location: (Type in console of chrome)
The location object has all the information about url
that being loaded.
• document : (Type in console of chrome)
The document object contains all oh html displayed
in page
Now type in node console:
• It displays undefined message
Why not work in node?
• Because node is not just an instance , so
“global” is defined in node but not in chrome
console wh...
• This global object has several functions (which
are not in chrome console)
• EG: require function: used to pull in diffe...
But error in chrome console:
Some works on both:
• Eg: console
Note: local variable vs global
• Eg:
name = prabin; //global
Var name = prabin; //local
Node is Non-blocking: Blocking vs Noblocking 1
Blocking vs No-blocking 2
Blocking vs No-blocking 3
Callback alternate syntax:
MODULES
REQUIRING MODULES
var http = require('http'); //http.js
va r fs = require('fs'); // fs.js
How does ‘require’ return the li...
Custom Module Example:
• Prabin_App(base folder)
– Example.js
var say = require(‘./say_hello_module.js’); //say is assigne...
LETS CREATE OUR OWN MODULE
• For finding modules:
NPM: THE USERLAND SEA
“Core” is small. “Userland” is large.

Package manager for node
• Comes with node
• Module Repositor...
INSTALLING A NPM MODULE
/Home/my_app
• $ npm install request
https://github.com/mikeal/request
Installs into local node_mo...
LOCAL VS GLOBAL MODULE
• Install modules with executables globally:
$ npm install coffee-script –g //global
$ coffee app.c...
FINDING MODULES
• From npm command line:
$ npm search module_name
Eg: $ npm search request
DEFINING YOUR DEPENDENCIES
my_app/package.json
{
"name": "My App",
"version": "1",
"dependencies":
{
"connect": "1.8.7"
}
...
• SEMANTIC VERSIONING:
Major Minor Patch
1 . 8 . 7
Test Time
NODE.JS : Hello world
Hello.js
var http = require('http');
http.createServer(function(request, response) {
response.writeH...
• Run the server:
$ node Hello.js
(Listening on port 8080...)
• View output
$ curl http://localhost:8080
(Hello World)
Event Loops 1
Event Loops 2
Long Running Process
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200);
...
TWO CALLBACKS HERE
var http = require('http');
request
http.createServer(function(request, response) {
response.writeHead(...
TWO CALLBACKS TIMELINE
(With blocking timeline)
NOTE: TYPICAL BLOCKING THINGS
• Calls out to web services
• Reads/Writes on the Database
• Calls to extensions
EVENTS IN THE DOM
• The DOM triggers Events you can listen for
those events
EVENTS IN NODE 1
• Many objects in Node emit events
EVENTS IN NODE 2
Also there is custom EventEmitter
var EventEmitter = require('events').EventEmitter;
var http = require('http');

http.createServer(function(request, response){ ... });

But what is really going on here?
htt...
Breaking It down
var http = require('http');
ALTERNATE SYNTAX
Streams
STREAMING RESPONSE
Readable stream

Writable stream

http.createServer(function(request, response) {
response.writeHead(20...
HOW TO READ FROM THE REQUEST?
$ curl -d 'hello' http://localhost:8080
Output on client: hello
READING AND WRITING A FILE
var fs = require('fs'); // require filesystem module
var file = fs.createReadStream("readme.md"...
UPLOAD A FILE
var fs = require('fs');
var http = require('http');
http.createServer(function(request, response) {
var newF...
DOCUMENTATION
http://nodejs.org/api/
REMEMBER THIS CODE 1?
var fs = require('fs');
var newFile = fs.createWriteStream("readme_copy.md");
var http = require('ht...
REMEMBER THIS CODE 2?
http.createServer(function(request, response) {
var newFile = fs.createWriteStream("readme_copy.md")...
Express
Express
SOCKET.IO
SOCKET.IO
• Traditional
Persisting Data
Persisting Data
References:
• IN T R O T O N O D E . J S (Authorized site)
• Ihrig C. J. - Pro Node.js for Developers
• Gackenheimer C. - ...
Thank You
• Any Suggestions and queries :
Website: http://prabinsilwal.com.np
Email: silwalprabin@hotmail.com
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
A complete guide to Node.js
Upcoming SlideShare
Loading in...5
×

A complete guide to Node.js

1,807
-1

Published on

A complete guide to start server side javascript programming called Node.js by Er. Prabin Silwal.

Published in: Education

A complete guide to Node.js

  1. 1. Complete Guide On Node.js By: Senior Developer Prabin Silwal
  2. 2. What you should know before? Javascript Unix command
  3. 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. 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. 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
  6. 6. Which is preferable? • For mine 2nd way
  7. 7. Window installation: • Download node.js • Click install
  8. 8. Checking after installation? • Run command • Type “node” and hit enter • Type “console.log(‘your text’)”
  9. 9. Note that previous installation process needs re installation for new updates.
  10. 10. Installing using nvm in Linux(Not wizard mode for Linux):
  11. 11. Make ready to install by following command: • sudo apt-get install git • sudo apt-get install curl
  12. 12. Install using command:
  13. 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. 14. What can you build? • • • • Websocket server ( Like chat server) Fast file upload client Ad server Any Real- Time Data Apps
  15. 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. 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. 17. • location: (Type in console of chrome) The location object has all the information about url that being loaded.
  18. 18. • document : (Type in console of chrome) The document object contains all oh html displayed in page
  19. 19. Now type in node console: • It displays undefined message
  20. 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. 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
  22. 22. But error in chrome console:
  23. 23. Some works on both: • Eg: console
  24. 24. Note: local variable vs global • Eg: name = prabin; //global Var name = prabin; //local
  25. 25. Node is Non-blocking: Blocking vs Noblocking 1
  26. 26. Blocking vs No-blocking 2
  27. 27. Blocking vs No-blocking 3
  28. 28. Callback alternate syntax:
  29. 29. MODULES
  30. 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. 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); });
  32. 32. LETS CREATE OUR OWN MODULE
  33. 33. • For finding modules:
  34. 34. 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”
  35. 35. 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
  36. 36. 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
  37. 37. FINDING MODULES • From npm command line: $ npm search module_name Eg: $ npm search request
  38. 38. 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
  39. 39. • SEMANTIC VERSIONING: Major Minor Patch 1 . 8 . 7
  40. 40. Test Time
  41. 41. 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...');
  42. 42. • Run the server: $ node Hello.js (Listening on port 8080...) • View output $ curl http://localhost:8080 (Hello World)
  43. 43. Event Loops 1
  44. 44. Event Loops 2
  45. 45. 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);
  46. 46. 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);
  47. 47. TWO CALLBACKS TIMELINE
  48. 48. (With blocking timeline)
  49. 49. NOTE: TYPICAL BLOCKING THINGS • Calls out to web services • Reads/Writes on the Database • Calls to extensions
  50. 50. EVENTS IN THE DOM • The DOM triggers Events you can listen for those events
  51. 51. EVENTS IN NODE 1 • Many objects in Node emit events
  52. 52. EVENTS IN NODE 2
  53. 53. Also there is custom EventEmitter var EventEmitter = require('events').EventEmitter;
  54. 54. var http = require('http'); http.createServer(function(request, response){ ... }); But what is really going on here? http://nodejs.org/api/
  55. 55. Breaking It down var http = require('http');
  56. 56. ALTERNATE SYNTAX
  57. 57. Streams
  58. 58. STREAMING RESPONSE Readable stream Writable stream http.createServer(function(request, response) { response.writeHead(200); response.write(“Hello World Writing."); setTimeout(function() { response.write(" Hello World is done."); response.end(); }, 5000); }).listen(8080); Output to client: Hello World Writing. (5 seconds later) Hello World is done.
  59. 59. HOW TO READ FROM THE REQUEST?
  60. 60. $ curl -d 'hello' http://localhost:8080 Output on client: hello
  61. 61. READING AND WRITING A FILE var fs = require('fs'); // require filesystem module var file = fs.createReadStream("readme.md"); var newFile = fs.createWriteStream("readme_copy.md"); require filesystem module file.pipe(newFile);
  62. 62. UPLOAD A FILE var fs = require('fs'); var http = require('http'); http.createServer(function(request, response) { var newFile = fs.createWriteStream("readme_copy.md"); request.on('end', function() { response.end('uploaded!'); }); }).listen(8080); On Client: $ curl --upload-file readme.md http://localhost:8080 Output on client: uploaded!
  63. 63. DOCUMENTATION http://nodejs.org/api/
  64. 64. REMEMBER THIS CODE 1? var fs = require('fs'); var newFile = fs.createWriteStream("readme_copy.md"); var http = require('http'); http.createServer(function(request, response) { request.pipe(newFile); request.on('end', function() { response.end('uploaded!'); }); }).listen(8080);
  65. 65. REMEMBER THIS CODE 2? http.createServer(function(request, response) { var newFile = fs.createWriteStream("readme_copy.md"); var fileBytes = request.headers['content-length']; var uploadedBytes = 0; request.pipe(newFile); request.on('data', function(chunk) { uploadedBytes += chunk.length; var progress = (uploadedBytes / fileBytes) * 100; response.write("progress: " + parseInt(progress, 10) + "%n"); }); }).listen(8080);
  66. 66. Express
  67. 67. Express
  68. 68. SOCKET.IO
  69. 69. SOCKET.IO • Traditional
  70. 70. Persisting Data
  71. 71. Persisting Data
  72. 72. References: • IN T R O T O N O D E . J S (Authorized site) • Ihrig C. J. - Pro Node.js for Developers • Gackenheimer C. - Node.js Recipes
  73. 73. Thank You • Any Suggestions and queries : Website: http://prabinsilwal.com.np Email: silwalprabin@hotmail.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×