var express = require('express'),
    parseString = require('xml2js').parseString,
    usps = require('./lib/usps'),
    verifyShipment = require('./lib/shipment').verify,
    createShipment = require('./lib/shipment').createShipmentFromReq,
    config = require('./config'),
    util = require('util');

/* Create App */

var app = express();

/* Config */

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'))

/* Routes */

app.get('/', function (req, res) {
    res.render('pages/index');
});

app.get('/rates-api', function(req, res, next) {
    var shipment = createShipment(req);

    //TODO: Attach and test this verify code!
    verifyShipment(shipment);

    usps.queryUSPS(shipment, function(err, apiQueryResults) {
        console.log("RESPONSE\n" + apiQueryResults);
        if (err) {
            /* THIS DOESN'T WORK RIGHT NOW
            responseData.errors.push(
                {
                    severity: "warning",
                    title: "Error:",
                    message: err.message
                }
            );
            */
            return next(err);
        }
        parseString(apiQueryResults, function(err, convertedResponse) {
            var responseData = { results:[], errors:[] };
            var USPSResults = usps.parseResponse(convertedResponse);
            responseData = {
                results: responseData.results.concat(USPSResults.results),
                errors: responseData.errors.concat(USPSResults.errors)
            }
            console.log(util.inspect(responseData,false,null));
            res.json(responseData);
        });
    });
});

/* Listen */

app.listen(config.port);
console.log("Server listening on port " + config.port);

@@ -692,24 +692,26 @@
 y code!%0A    
+//
 verifyShipme
@@ -852,61 +852,72 @@
     
-if (err) %7B%0A            /* THIS DOESN'T WORK RIGHT NOW
+var responseData = %7B results:%5B%5D, errors:%5B%5D %7D;%0A        if (err) %7B
 %0A   
@@ -1137,23 +1137,10 @@
     
-*/%0A            
+//
 retu
@@ -1154,33 +1154,44 @@
 (err);%0A        %7D
-%0A
+ else %7B%0A    
         parseStr
@@ -1254,62 +1254,8 @@
     
-        var responseData = %7B results:%5B%5D, errors:%5B%5D %7D;%0A
     
@@ -1319,16 +1319,20 @@
 ponse);%0A
+    
         
@@ -1368,16 +1368,20 @@
         
+    
 results:
@@ -1447,16 +1447,20 @@
         
+    
 errors: 
@@ -1510,34 +1510,42 @@
 rs)%0A            
+ 
-%7D%0A
+   %7D%0A    
             cons
@@ -1592,24 +1592,46 @@
 null));%0A    
+        %7D);%0A        %7D%0A
         res.
@@ -1650,28 +1650,16 @@
 eData);%0A
-        %7D);%0A
     %7D);%0A

Fix error handling of usps request