var http    = require('http'),
    express = require('express'),
    app     = express(),
    libxmljs= require('libxmljs');

var zwid = 'X1-ZWz1dp4jgzyih7_axb7v';

app.configure(function() {

    // simple logger
    app.use(function(req, res, next){
    	console.log('%s %s', req.method, req.url);
	    next();
    });

    app.use(express.compress());
    // app.use(express.cookieParser('xxx secret'));
    // app.use(express.cookieSession());
    // app.use(express.bodyParser());
    // app.use(app.router);

});

app.get('/propertySearch', function(req, client) {
	var ec = encodeURIComponent;
	var q = "?zws-id="+ec(zwid)+"&address="+ec(req.query.address)+
								"&citystatezip="+ec(req.query.citystatezip);
	var options = {
	  host: 'www.zillow.com',
	  path: '/webservice/GetSearchResults.htm' + q
	};

	var req = http.get(options, function(res) {
	  // console.log('STATUS: ' + res.statusCode);
	  // console.log('HEADERS: ' + JSON.stringify(res.headers));

	  // Buffer the body entirely for processing as a whole.
	  var bodyChunks = [];
	  res.on('data', function(chunk) {
	    // You can process streamed parts here...
	    bodyChunks.push(chunk);
	  }).on('end', function() {
	    var body = Buffer.concat(bodyChunks);
	    // console.log('BODY: ' + body);

	    var xmlDoc = libxmljs.parseXml(body);
	    var out = JSON.stringify(xmlDoc);

	    console.log(out);
	    client.status(200).set('Content-Type', 'text/html').send(out);

	    // ...and/or process the entire body here.
	  })
	});

	req.on('error', function(e) {
	  console.log('ERROR: ' + e.message);
	  client.status(200).set('Content-Type', 'text/html').send('fail');
	});
});

app.get('/propertyDetail', function(req, res) {
	var uri = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id="+zwid+"&zpid="+req.query.zpid;

	http.get(encodeURIComponent(uri), function(res){
		console.log(res);
	    res.status(200).set('Content-Type', 'text/html').send('ok');
	});
});


var server = http.createServer(app);
server.listen(8080);
module.exports = server;


@@ -87,24 +87,23 @@
 (),%0A    
-lib
 xml
+2
 js
+%09
 = requir
@@ -109,21 +109,53 @@
 re('
-lib
 xml
+2
 js');%0A%0A
+var parser = new xml2js.Parser();%0A
 var 
@@ -999,66 +999,8 @@
 );%0A%0A
-%09  // Buffer the body entirely for processing as a whole.%0A
 %09  v
@@ -1059,55 +1059,8 @@
 ) %7B%0A
-%09    // You can process streamed parts here...%0A
 %09   
@@ -1200,90 +1200,60 @@
 ;%0A%0A%09
-    var xmlDoc = libxmljs.parseXml(body);%0A%09    var out = JSON.stringify(xmlDoc);%0A%0A
+%09parser.parseString(data, function (err, result) %7B%0A%09
 %09   
@@ -1265,18 +1265,22 @@
 ole.
-log(ou
+dir(resul
 t);%0A
+%09
 %09   
@@ -1341,62 +1341,49 @@
 end(
-ou
+resul
 t);%0A
-%0A
+%09
 %09    
-// ...and/or process the entire body here.
+console.log('Done');%0A%09%09%7D);
 %0A%09  

fix xml parsing