hacking with node.JS
-
Upload
harsha-vashisht -
Category
Technology
-
view
7.477 -
download
0
description
Transcript of hacking with node.JS
![Page 1: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/1.jpg)
![Page 2: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/2.jpg)
#hacking with node.JSHarsha Vashishthttp://www.harsharv.com@harsharvPicture Source: http://bethesignal.org/blog/2011/01/06/speaking-lca2011-html5-nodejs/
![Page 3: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/3.jpg)
WHAT IS NODE.JS?
![Page 4: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/4.jpg)
Node is an exciting new platform developed byRyan Dahl.
![Page 5: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/5.jpg)
IT IS A LOT OF SUGAR ON V8
![Page 6: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/6.jpg)
It allows JavaScript developers to create extremely high performance servers by leveraging Google's V8 JavaScript engine, and asynchronous I/O.
![Page 7: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/7.jpg)
NEVER WAIT IT THE MOTTO
![Page 8: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/8.jpg)
RECAP… WHAT IS NODE.JS?
•Node is an exciting new platform developed by Ryan Dahl.
•It allows JavaScript developers to create extremely high performance servers by leveraging Google's V8 JavaScript engine, and asynchronous I/O.
•Node is a lot of Sugar written on V8
•Never wait is the motto. Either to fetch a page from the web or fetch data from a DB.
![Page 9: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/9.jpg)
LET US LOOK AT…
![Page 10: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/10.jpg)
LETS SEE COD
DUCK! There will be code…
![Page 11: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/11.jpg)
1. Order2. Waits for the foodto be cooked
4. Serve3.Food is ready
LAMP STACK
![Page 12: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/12.jpg)
1. Order2. Places the orderwith the cook3. Immediately moves on to take the order from the next table4. Cook notifies that the food is ready5. Food is served
Node JS
![Page 13: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/13.jpg)
COMMONJS
CommonJS is a community driven effort to standardize packaging of JavaScript libraries, known as modules. Modules written which comply to this standard provide portability between other compliant frameworks such as narwhal, and in some cases even browsers.*
* Conditions Apply
![Page 14: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/14.jpg)
WHY NODE.JS?
![Page 15: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/15.jpg)
GLOBAL OBJECTS
•Require
•Module/ export
•Process
![Page 16: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/16.jpg)
REQUIRE
This is used to specify that a module is required.
Example:
var operation = require(“pow”)
![Page 17: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/17.jpg)
MODULE/ EXPORTS
This is used to package JavaScript libraries and expose the functionality to be used else where.
Example:
exports.pow = function (a, b) {
…
}
![Page 18: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/18.jpg)
PROCESS
•The process object is a global object and can be accessed from anywhere.
•It is an instance of EventEmitter.
•Can be thought as an equivalent to Window object in the browser
Examples:•process.cwd()
•process.exit(1)
•process.argv
•process.on('exit', function () {
...}
•process.on('uncaughtException', function (err) {
…}
![Page 19: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/19.jpg)
SIMPLE HTTP SERVER
/* File Name: hello-server.js */
/* Include the http module. */
var http = require('http');
/* Create a server which accepts Request - req and returns Response - res */
http.createServer(function (req, res) {
/* Set the HTTP header for the response. */
res.writeHead(200, {'Content-Type': 'text/plain'});
/* End the HTTP response */
res.end('Hello World\n');
}).listen(9000, "127.0.0.1");
/* Print a message to indicate that the server is running. */
console.log('Server running at http://127.0.0.1:9000/');
![Page 20: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/20.jpg)
MODULES LETS RIGHT SOME
![Page 21: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/21.jpg)
CALCULATE POWER OF A NUMBER
exports.pow = function (a, b) {
/* Check if a number is passed. */
if (isNaN (a-0) || isNaN (b-0)) {
return null;
}
var ans = 1;
for (var i = 1; i <= b; i++) {
ans *= a;
}
return ans;
}Link: https://github.com/harsharv/OpenHackDay2011/blob/master/hello-server.js
![Page 22: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/22.jpg)
TWITTER SEARCH MODULE
https://github.com/harsharv/OpenHackDay2011/blob/master/twitter.js
https://github.com/harsharv/OpenHackDay2011/blob/master/mashup.js
![Page 23: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/23.jpg)
NPM – NODE PACKAGE MANAGER
•npm is the package manager for the Node JavaScript platform
•It puts modules in place so that node can find them, and manages dependency conflicts intelligently.
![Page 24: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/24.jpg)
MORE ABOUT NPM
•Install NPM
curl http://npmjs.org/install.sh | sh
•Browse the packages of NPMhttp://search.npmjs.org/
![Page 25: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/25.jpg)
NPM BASIC COMMANDS
•npm search
•npm ls
•npm install yui3
•npm uninstall yui3
![Page 26: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/26.jpg)
YUI3 AND NODE.JS
![Page 27: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/27.jpg)
RESOURCES
•http://nodejs.org/
•http://npmjs.org/
•http://www.yuiblog.com/blog/2010/04/05/running-yui-3-server-side-with-node-js/
•http://developer.yahoo.com/yui/3/
•http://github.com/davglass/nodejs-yui
•http://github.com/davglass/yui-express
•http://github.com/harsharv/OpenHackDay2011
![Page 28: hacking with node.JS](https://reader035.fdocuments.in/reader035/viewer/2022062220/555c2483d8b42a09438b4b6a/html5/thumbnails/28.jpg)
Harsha R. [email protected]
http://www.harsharv.comhttp://twitter.com/harsharv
http://www.facebook.com/harsharvhttp://www.slideshare.net/harsharv
http://github.com/harsharv/