title
[previously]
- [2015-06-04] TRY = facebook example (contd)
- ] # # -
- [2015-06-04] TRY = my endpoint
- ] # # - rather then graph.facebook USE my own endpoint
- [2015-06-04] TRY = blue tutorial example
- ] # 5229 - built a subset of the entire
- [2015-06-04] status @ hours = 00:00
- ] still whatever method to return object is coming up as undefined
- ] WORKS - hardcode json array in "index.js"
- ] WORKS - app require (hardcoded json file), write file (copy and past)
- [2015-06-05] TRY = use pre built library (Request || )
- ]
[currently]
- ] JSON.parse [JSON to OBJECT]- can be used to convert string into a JSON text into a JavaScript object:
var obj = json.parse()
- ] stringify [OBJECT to JSON]
[next]
- ] REFERENCE
- ] lrn node on windows -
- ] DOC node https://nodejs.org/api/http.html#http_http_get_options_callback
- ] DOC express - http://expressjs.com/api.html#res.json
- ] GUIDE http://blog.mashape.com/30-ways-to-make-rest-calls-in-node-js-php-python/
- ] example code - using google maps api -
- 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
- ] example code - using facebook graph api -
- ] example code - more robuts, full featured, pro
- ] example code - HTTP GET Request in Node.js Express -bryan mcfarlane, links to github repo
- ] example code - using static json - with blog post - http://blog.modulus.io/nodejs-and-express-create-rest-api
- ] example code - tutorial - MDN guy with even better version in comments
- ] install request - popular module for making api requests, easier, pipe data to files
- ] tutorial request module -
- ] tutorial request module - working with gzip compressed data,
- ] example code - writing files - SRC = g qry=nodejs write file , res# SO?
- CC
- ] 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 })
});