task

title

[previously]

  1. [2015-06-04] TRY = facebook example (contd)
    1. ] # # -   
  2. [2015-06-04] TRY = my endpoint
    1. ] # # - rather then graph.facebook USE my own endpoint 
  3. [2015-06-04] TRY = blue tutorial example
    1. ]  # 5229 - built a subset of the entire
  4. [2015-06-04] status @ hours = 00:00
    1. ] still whatever method to return object is coming up as undefined
    2. ] WORKS - hardcode json array in "index.js"
    3. ] WORKS - app require (hardcoded json file), write file (copy and past) 
  5. [2015-06-05] TRY = use pre built library (Request || ) 
    1. ]

[currently]

  1. ]  JSON.parse [JSON to OBJECT]- can be used to convert string into a JSON text into a JavaScript object:
    var obj = json.parse()
  2. ] stringify [OBJECT to JSON]

[next]

  1. ] REFERENCE
  2. ] lrn node on windows -
  3. ] DOC node https://nodejs.org/api/http.html#http_http_get_options_callback 
  4. ] DOC express - http://expressjs.com/api.html#res.json 
  5. ] GUIDE http://blog.mashape.com/30-ways-to-make-rest-calls-in-node-js-php-python/ 
  6. ] example code - using google maps api -  
    1. Yes, Node.js is perfectly suited to making calls to external APIs. Just like everything in Node, however, 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. Robert Mitchell
  7. ] example code - using facebook graph api -
  8. ] example code - more robuts, full featured, pro 
  9. ] example code - HTTP GET Request in Node.js Express -bryan mcfarlane, links to github repo
  10. ] example code - using static json - with blog post - http://blog.modulus.io/nodejs-and-express-create-rest-api 
  11. ] example code - tutorial - MDN guy with even better version in comments
  12. ] install request - popular module for making api requests, easier, pipe data to files
  13. ] tutorial request module -
  14. ] tutorial request module - working with gzip compressed data,
  15. ] example code - writing files - SRC = g qry=nodejs write file , res# SO?
  16. CC
    1. ] BM /node/http-request api USING request package

 

 

      have hardcoded an array of json values named quotes,

      have defined a view named rankings which contains template  

// view - results
    app.get('/rankingsD', function (req, res) {
        /* PASS - this sends the json to the browser as a file
        res.json(quotes);
 */
 
        /* PASS - pass a single variable into the template
 var msg = { msg: "hello from RankingsD" }; 
 res.render('rankingsD', { title: 'Ranking Drivers ', quotes: quotes, msg: msg }) 
 */
 
        /* TRY - example from Sanderson, db is a user defined(UD), seperate module, has function named load, which retuns 
 var msg = { msg: "hello from RankingsD" };
        var results = db.load(req.params.id); 
 */
        var url = 'http://graph.facebook.com/517267866/?fields=picture';
        // var results = app.get(url);
        // var results = { url: "1234" };
 
        /*NOTE - most example code uses http.get (a node method) VS app.get */
        app.get(url, function (res) {
            var body = '';
            res.on('data', function (chunk) {
                body += chunk;
            });
        res.on('end', function () {
            var fbResponse = JSON.parse(body);
            res.send(fbResponse)
            // ADD
            // return results = JSON.parse(body) // NO errors, returns undefined
            console.log("Got response: ", fbResponse.picture);
         });
        }).on('error', function (e) {
            console.log("Got error: ", e);
        });
 
 
        //var results = getData();
 
        res.render('rankingsD', { title: 'Ranking Drivers ', quotes: quotes })
 
 
    });

 

Details Photos Edit more

Details

ID: 5228

NAME: CREATE-article-call-api-from-node-express

DESCRIPTION: ]

START DATE TIME: 2015-06-04 06:56:55

EST DURATION: 01:00:00

END DATE TIME: 2015-06-04 07:56:55

STATUS:

PRIORITY: -5

OWNER ID: 1

Content Photos Edit more

photos

photos for this task

actions

Agenda Email task SMS task Priorities