Download - hacking with node.JS
#hacking with node.JSHarsha Vashishthttp://www.harsharv.com@harsharvPicture Source: http://bethesignal.org/blog/2011/01/06/speaking-lca2011-html5-nodejs/
WHAT IS NODE.JS?
Node is an exciting new platform developed byRyan Dahl.
IT IS A LOT OF SUGAR ON V8
It allows JavaScript developers to create extremely high performance servers by leveraging Google's V8 JavaScript engine, and asynchronous I/O.
NEVER WAIT IT THE MOTTO
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.
LET US LOOK AT…
LETS SEE COD
DUCK! There will be code…
1. Order2. Waits for the foodto be cooked
4. Serve3.Food is ready
LAMP STACK
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
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
WHY NODE.JS?
GLOBAL OBJECTS
•Require
•Module/ export
•Process
REQUIRE
This is used to specify that a module is required.
Example:
var operation = require(“pow”)
MODULE/ EXPORTS
This is used to package JavaScript libraries and expose the functionality to be used else where.
Example:
exports.pow = function (a, b) {
…
}
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) {
…}
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/');
MODULES LETS RIGHT SOME
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
TWITTER SEARCH MODULE
https://github.com/harsharv/OpenHackDay2011/blob/master/twitter.js
https://github.com/harsharv/OpenHackDay2011/blob/master/mashup.js
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.
MORE ABOUT NPM
•Install NPM
curl http://npmjs.org/install.sh | sh
•Browse the packages of NPMhttp://search.npmjs.org/
NPM BASIC COMMANDS
•npm search
•npm ls
•npm install yui3
•npm uninstall yui3
YUI3 AND NODE.JS
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
Harsha R. [email protected]
http://www.harsharv.comhttp://twitter.com/harsharv
http://www.facebook.com/harsharvhttp://www.slideshare.net/harsharv
http://github.com/harsharv/