Node

Preview:

Citation preview

Fetch Statistics from the Yahoo Finance and save it

into a Google Sheet document

• An Engineering Student at the International Institute of Technology

• Article Writer at CodeProject.com

About me

• A powerful platform to let you run JS on the server side• How? Uses Google’s V8 Engine• V8 is built in C• V8 is the fatest JS Engine on the planet• Great way to build modern web apps on both Client and

Server side!

What is Node.js?

Timeline

2009 2010 2011 2012

Jan 2009Created Ryan Dahl

April 2010 Heroku launches node support

Nov 2011 Windows Azure support

EBay releases API built on node

Cloud9IDE azuresupport

Oct 2011 node.js overtakes Ruby as most popular repo on gitHub

Walmart Launch mobile site on node.js

Feb 2012 App Harbour support

July 2011

LinkedIn adopts node for mobile platform

port to Windows

IISNode

July 2010 Yammer adopts node.js

Nov 2010 Cloud9IDE launches

Companies support Node.js

• Yahoo!• Microsoft • LinkedIn• Ebay• Fidelizoo• Paypal• CHESS• …

What Can I Do in Node?

• Anything you want!• Chat servers, Analytics & Crazy fast backends• Socket.io library is a wicked way to build real time apps• Build a social Network! LinkedIn, Dropbox all using Node.js

What Can’t I Do in Node?

• Contradicts previous slide but Node.js is not a web framework i.e Sinatra Modules for node.js make it into web framework i.e Express Node.js is not Multi-threaded A single thread to rule them all

Non-Blocking? Blocking? I’m so confused

• By introducing callbacks. Node can move on to other requests and whenever the callback is called, node will process is.

• You should read non-blocking code as « put function and params in queue and fire callback when you reach the end of the queue »

• Blocking= return Non-Blocking= no return. Only callbacks

Fetch Statistics from the Yahoo Finance

• We have to fetch data from the link shown below:

http://finance.yahoo.com/q/ks?s=LVS

The Company in our example is LVS.

• You need to install Node.js Of course and it 3 packages: npm install request – So we could work with URLs in an easy

way. npm install cheerio – jQuery for the server side. This will make

the code 10x shorter. npm install edit-google-spreadsheet – to integrate with

Google docs/sheets with 2-3 lines of code.

Fetch Statistics from the Yahoo Finance

Fetch Statistics from the Yahoo Finance

• // Some parameters • var ticker = "LVS";• var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;• var financeDetails = new Array();• var keyStr = new Array();

• // The main call to fetch the data, parse it and work on it.

• request(yUrl, function (error, response, body) {• if (!error && response.statusCode == 200) {• var $ = cheerio.load(body);• // the keys - We get them from a certain class attribute• var td = $('.yfnc_tablehead1');• $(td).each(function(j, val) {• keyStr[j] = $(val).text();• });

Fetch Statistics from the Yahoo Finance

Working with Google Sheet Document

• // upload our data to Google sheet• Spreadsheet.create({• debug: true,• username: 'TODO-fill',• password: 'TODO-fill',• debug: true,• spreadsheetName: 'TODO-yourSheetName',• worksheetName: 'TODO-Sheet1orAbetterName',• callback: sheetReady• });

Demo

Thank you

hadrichmed@gmail.com

Recommended