+ All Categories
Home > Technology > hacking with node.JS

hacking with node.JS

Date post: 20-May-2015
Category:
Upload: harsha-vashisht
View: 7,479 times
Download: 0 times
Share this document with a friend
Description:
Getting started with node.JS
Popular Tags:
28
Transcript
Page 1: hacking with node.JS
Page 2: 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/

Page 3: hacking with node.JS

WHAT IS NODE.JS?

Page 4: hacking with node.JS

Node is an exciting new platform developed byRyan Dahl.

Page 5: hacking with node.JS

IT IS A LOT OF SUGAR ON V8

Page 6: hacking with node.JS

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

NEVER WAIT IT THE MOTTO

Page 8: hacking with node.JS

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

LET US LOOK AT…

Page 10: hacking with node.JS

LETS SEE COD

DUCK! There will be code…

Page 11: hacking with node.JS

1. Order2. Waits for the foodto be cooked

4. Serve3.Food is ready

LAMP STACK

Page 12: hacking with node.JS

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

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

WHY NODE.JS?

Page 15: hacking with node.JS

GLOBAL OBJECTS

•Require

•Module/ export

•Process

Page 16: hacking with node.JS

REQUIRE

This is used to specify that a module is required.

Example:

var operation = require(“pow”)

Page 17: hacking with node.JS

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

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

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

MODULES LETS RIGHT SOME

Page 21: hacking with node.JS

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

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

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

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

NPM BASIC COMMANDS

•npm search

•npm ls

•npm install yui3

•npm uninstall yui3

Page 26: hacking with node.JS

YUI3 AND NODE.JS

Page 27: hacking with 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

Page 28: hacking with node.JS

Harsha R. [email protected]

http://www.harsharv.comhttp://twitter.com/harsharv

http://www.facebook.com/harsharvhttp://www.slideshare.net/harsharv

http://github.com/harsharv/


Recommended