var express = require('express');
var router = express.Router();
var request = require('request');

/* GET home page. */
router.get('/', function(req, res, next) {

  if(req.query.cardnumber != undefined && req.query.cardnumber != null && req.query.pin != undefined && req.query.pin != null){

  	request.post({url:'https://www.starbucks.com/card/guestbalance', form: {"Card.Number":req.query.cardnumber,"Card.Pin":req.query.pin}}, function(err,httpResponse,body){ 

  		if(body != undefined && body != null){

		  	try{
		  		var fetchBalanceDiv = (body.split('<div id="fetch_balance" class="region size1of3 reset">')[1]).split('</div>')[0];

		  		var cardBalance = (fetchBalanceDiv.split('<h2><span class="fetch_balance_value">')[1]).split('</span>')[0];

		  		var lastUpdated = (fetchBalanceDiv.split('<p>as of <span class="date">')[1]).split('</span>')[0];

		  		console.log(cardBalance + " " + lastUpdated);

		  		var data = 
		  		{
		  			bal:cardBalance,
		  			lastUpdated:lastUpdated
		  		}
		  		res.send(data);

		  	}catch(err){
		  		res.send('could not find card');
		  	};

  		}else{
  			res.send('could not find card');
  		}
  	

  	});

  }else{
  	res.render('index', { title: 'SBUX Card API' });
  }
});

module.exports = router;

@@ -169,32 +169,33 @@
 f(req.query.card
+_
 number != undefi
@@ -211,24 +211,25 @@
 q.query.card
+_
 number != nu
@@ -392,16 +392,17 @@
 ery.card
+_
 number,%22
@@ -1058,37 +1058,92 @@
 es.send(
-'could not find card'
+%7B%0A%09%09  %09%09%09bal:%22Error: Could not lookup card%22,%0A%09%09  %09%09%09lastUpdated:null%0A%09%09  %09%09%7D
 );%0A%09%09  %09

card_number not cardnumber