Last active
March 18, 2016 19:31
-
-
Save shduff/31fe3afd25ea3faa9a81 to your computer and use it in GitHub Desktop.
grabbing apple info from tradeking and looking at it!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title></title> | |
| </head> | |
| <body> | |
| <button id='aapl'>AAPL</button> | |
| <script> | |
| var myStockData = {}; | |
| // important that any data you want accessible in your document gets stored in a global variable so you can inject it into your page | |
| function callback(data) { | |
| var response = JSON.parse(data.target.responseText) | |
| var ticker = response.quotes.quote.symbol; | |
| myStockData[ticker] = response.quotes.quote; | |
| console.log(ticker); | |
| } | |
| var aapl = document.getElementById('aapl'); | |
| aapl.addEventListener('click', function(e) { | |
| var oReq = new XMLHttpRequest(); | |
| oReq.addEventListener("load", callback); | |
| oReq.open("GET", "http://localhost:8080/api/stock/AAPL"); | |
| oReq.send(); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var app = express(); | |
| var morgan = require('morgan'); // log requests to the console (express4) | |
| var bodyParser = require('body-parser'); // pull information from HTML POST (express4) | |
| var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) | |
| var path = require('path'); | |
| var request = require('request'); | |
| var oauth = require('oauth'); | |
| var tradeKingConfig = require(path.join(__dirname, '.', 'config')); | |
| var tradeking_consumer = new oauth.OAuth( | |
| "https://developers.tradeking.com/oauth/request_token", | |
| "https://developers.tradeking.com/oauth/access_token", | |
| tradeKingConfig.consumer_key, | |
| tradeKingConfig.consumer_secret, | |
| "1.0", | |
| null, | |
| "HMAC-SHA1"); | |
| // configuration =========================== | |
| app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users | |
| app.use(morgan('dev')); // log every request to the console | |
| app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded | |
| app.use(bodyParser.json()); // parse application/json | |
| app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json | |
| app.use(methodOverride()); | |
| // routes ================================== | |
| app.get("/api/stock/:symbol", function(req,res){ | |
| var url = tradeKingConfig.api_url+'market/ext/quotes.json?symbols=' + req.params.symbol; | |
| tradeking_consumer.get(url, tradeKingConfig.access_token, tradeKingConfig.access_secret, | |
| function(error, data, response) { | |
| // Parse the JSON data | |
| stock_data = JSON.parse(data); | |
| // Display the response | |
| console.log(stock_data.response); | |
| // simple answer for how to get data from backend to frontend! | |
| res.send(stock_data.response); | |
| } | |
| ); | |
| }); | |
| // longer answer for how to get data from the backend to frontend | |
| app.get('/', function(req,res) { | |
| res.sendFile('index.html', {root:'.'}); | |
| // need to update express 4.8.0, change package.json and run npm install | |
| // Note: eventually you may want to use a templating engine | |
| // (mustache or jade are two recommended for use with express) | |
| // look into res.render() and particular config for that templating engine | |
| }) | |
| app.listen(8080); | |
| console.log("App listening on port 8080"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment