edit-task
Home
Up
Delete
Task Name:
Task Description:
]
TaskGroup ID:
Start Date:
Start Time:
Duration:
Priority:
Status:
To Do
Completed
In Process
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">title</h1> <h2>[previously]</h2> <ol> <li><strong>[2015-06-04] TRY = facebook example (contd)</strong></li> <ol> <li>] # # - </li> </ol> <li><strong>[2015-06-04] TRY = my endpoint</strong></li> <ol> <li>] # # - rather then graph.facebook USE my own endpoint </li> </ol> <li><strong>[2015-06-04] TRY = blue tutorial example</strong></li> <ol> <li>] # 5229 - built a subset of the entire</li> </ol> <li><strong>[2015-06-04] status @ hours = 00:00</strong></li> <ol> <li>] still whatever method to return object is coming up as undefined</li> <li>] WORKS - hardcode json array in "index.js"</li> <li>] WORKS - app require (hardcoded json file), write file (copy and past) </li> </ol> <li><strong>[2015-06-05] TRY = use pre built library (Request || ) </strong></li> <ol> <li>]</li> </ol></ol> <h2>[currently]</h2> <ol> <li>] <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" target="_blank">JSON.parse</a> [JSON to OBJECT]- can be used to convert string into a JSON text into a JavaScript object:<br />var obj = json.parse()</li> <li>] <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" target="_blank">stringify</a> [OBJECT to JSON]</li> </ol> <h2>[next]</h2> <ol> <li><strong>] REFERENCE</strong></li> <li>] <a href="/view/task?id=5218" target="_blank">lrn node</a> on windows -</li> <li>] DOC node <a href="https://nodejs.org/api/http.html#http_http_get_options_callback" target="_blank">https://nodejs.org/api/http.html#http_http_get_options_callback</a> </li> <li>] DOC express - <a href="http://expressjs.com/api.html#res.json" target="_blank">http://expressjs.com/api.html#res.json</a> </li> <li>] GUIDE <a href="http://blog.mashape.com/30-ways-to-make-rest-calls-in-node-js-php-python/" target="_blank">http://blog.mashape.com/30-ways-to-make-rest-calls-in-node-js-php-python/</a> </li> <li>] example code - <a href="http://stackoverflow.com/questions/16148403/using-node-js-to-connect-to-a-rest-api/16155551#16155551" target="_blank">using google maps api</a> - </li> <ol> <li>Yes, Node.js is perfectly suited to making calls to external APIs. Just like everything in Node, however, <span style="background-color: #00ff00;">the functions for making these calls are based around events, which means doing things like buffering response data as opposed to receiving a single completed response</span>. Robert Mitchell</li> </ol> <li>] example code - <a href="http://stackoverflow.com/questions/11826384/calling-a-json-api-with-node-js" target="_blank">using facebook graph api</a> -</li> <li>] example code - <a href="http://stackoverflow.com/questions/9577611/http-get-request-in-node-js-express" target="_blank">more robuts, full featured, pro</a> </li> <li>] example code - <a href="http://stackoverflow.com/questions/9577611/http-get-request-in-node-js-express" target="_blank">HTTP GET Request in Node.js Express</a> -bryan mcfarlane, links to github repo</li> <li>] example code - using static json - with blog post - <a href="http://blog.modulus.io/nodejs-and-express-create-rest-api" target="_blank">http://blog.modulus.io/nodejs-and-express-create-rest-api</a> </li> <li>] example code - <a href="http://davidwalsh.name/nodejs-http-request" target="_blank">tutorial</a> - MDN guy with even better version in comments</li> <li>] install <a href="https://github.com/request/request" target="_blank">request</a> - popular module for making api requests, easier, pipe data to files</li> <li>] <a href="http://blog.modulus.io/node.js-tutorial-how-to-use-request-module" target="_blank">tutorial request module</a> -</li> <li>] <a href="http://samwize.com/2013/08/31/simple-http-get-slash-post-request-in-node-dot-js/" target="_blank">tutorial request module</a> - working with gzip compressed data,</li> <li>] example code - <a href="http://stackoverflow.com/questions/2496710/writing-files-in-node-js" target="_blank">writing files</a> - SRC = g qry=nodejs write file , res# SO?</li> <li>CC<ol> <li>] BM /node/http-request api USING request package</li> </ol></li> </ol> <p> </p> <p> </p> <p> have hardcoded an array of json values named quotes,</p> <p> have defined a view named rankings which contains template </p> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;">// view - results</span> app.get<span style="color: #1e7c70;">(</span><span style="color: #823125;">'/rankingsD'</span><span style="color: #1e7c70;">,</span> <span style="color: #4f76ac;">function</span> <span style="color: #1e7c70;">(</span>req<span style="color: #1e7c70;">,</span> res<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> <span style="color: #46830d;">/* PASS - this sends the json to the browser as a file</span> <span style="color: #46830d;">res.json(quotes);</span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> */</span> <span style="color: #46830d;">/* PASS - pass a single variable into the template</span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> var msg = { msg: "hello from RankingsD" }; </span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> res.render('rankingsD', { title: 'Ranking Drivers ', quotes: quotes, msg: msg }) </span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> */</span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"> </pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"> <span style="color: #46830d;">/* TRY - example from Sanderson, db is a user defined(UD), seperate module, has function named load, which retuns </span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> var msg = { msg: "hello from RankingsD" };</span> <span style="color: #46830d;">var results = db.load(req.params.id); </span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #46830d;"> */</span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"> <span style="color: #4f76ac;">var</span> url <span style="color: #1e7c70;">=</span> <span style="color: #823125;">'http://graph.facebook.com/517267866/?fields=picture'</span><span style="color: #1e7c70;">;</span> <span style="color: #46830d;">// var results = app.get(url);</span> <span style="color: #46830d;">// var results = { url: "1234" };</span> <span style="color: #46830d;">/*NOTE - most example code uses http.get (a node method) VS app.get */</span> app.get<span style="color: #1e7c70;">(</span>url<span style="color: #1e7c70;">,</span> <span style="color: #4f76ac;">function</span> <span style="color: #1e7c70;">(</span>res<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">var</span> body <span style="color: #1e7c70;">=</span> <span style="color: #823125;">''</span><span style="color: #1e7c70;">;</span> res.on<span style="color: #1e7c70;">(</span><span style="color: #823125;">'data'</span><span style="color: #1e7c70;">,</span> <span style="color: #4f76ac;">function</span> <span style="color: #1e7c70;">(</span>chunk<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> body <span style="color: #1e7c70;">+=</span> chunk<span style="color: #1e7c70;">;</span> <span style="color: #1e7c70;">});</span></pre> <pre style="background: white; color: black; font-family: Consolas; font-size: 10pt;"> res.on<span style="color: #1e7c70;">(</span><span style="color: #823125;">'end'</span><span style="color: #1e7c70;">,</span> <span style="color: #4f76ac;">function</span> <span style="color: #1e7c70;">()</span> <span style="color: #1e7c70;">{</span> <span style="color: #4f76ac;">var</span> fbResponse <span style="color: #1e7c70;">=</span> <span style="color: #4f76ac;">JSON</span>.parse<span style="color: #1e7c70;">(</span>body<span style="color: #1e7c70;">);</span> res.send<span style="color: #1e7c70;">(</span>fbResponse<span style="color: #1e7c70;">)</span> <span style="color: #46830d;">// ADD</span> <span style="color: #46830d;">// return results = JSON.parse(body) // NO errors, returns undefined</span> console.log<span style="color: #1e7c70;">(</span><span style="color: #823125;">"Got response: "</span><span style="color: #1e7c70;">,</span> fbResponse.picture<span style="color: #1e7c70;">);</span> <span style="color: #1e7c70;">});</span> <span style="color: #1e7c70;">})</span>.on<span style="color: #1e7c70;">(</span><span style="color: #823125;">'error'</span><span style="color: #1e7c70;">,</span> <span style="color: #4f76ac;">function</span> <span style="color: #1e7c70;">(</span>e<span style="color: #1e7c70;">)</span> <span style="color: #1e7c70;">{</span> console.log<span style="color: #1e7c70;">(</span><span style="color: #823125;">"Got error: "</span><span style="color: #1e7c70;">,</span> e<span style="color: #1e7c70;">);</span> <span style="color: #1e7c70;">});</span> <span style="color: #46830d;">//var results = getData();</span> res.render<span style="color: #1e7c70;">(</span><span style="color: #823125;">'rankingsD'</span><span style="color: #1e7c70;">,</span> <span style="color: #1e7c70;">{</span> title<span style="color: #1e7c70;">:</span> <span style="color: #823125;">'Ranking Drivers '</span><span style="color: #1e7c70;">,</span> quotes<span style="color: #1e7c70;">:</span> quotes <span style="color: #1e7c70;">})</span> <span style="color: #1e7c70;">});</span></pre> <p> </p>