Created
August 10, 2010 07:59
-
-
Save ivarvong/516884 to your computer and use it in GitHub Desktop.
This file contains 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 sys = require('sys'); | |
var fs = require('fs'); | |
var connect = require('./connect/lib/connect'); | |
var http = require('http'); | |
function getDirections(data, callback) { | |
requestStr = "/m/directions?dirflg=r&saddr=" + escape(data.from) + "&daddr=" + escape(data.to); | |
var client = http.createClient(80, 'www.google.com'); | |
var request = client.request('GET', requestStr, {'host': 'www.google.com'}); | |
request.end(); | |
request.on('response', function (response) { | |
response.setEncoding('utf8'); | |
var responseBody = ""; | |
response.on('data', function (chunk) { | |
responseBody += chunk; | |
}); | |
response.on('end', function(data) { | |
callback({ rawHTML: responseBody, | |
firstRE: responseBody.match(/<div class="c"[^>]*>(.*?)<\/div>/g), | |
debug: "debug message" }); | |
}); | |
}); | |
} | |
function main(app) { | |
app.get('/', function(req, res){ | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
getDirections( { | |
to:'wow hall, eugene, or', | |
from:'13th and university, eugene, or' | |
}, function(data) { | |
data.firstRE.forEach(function(item) { | |
res.write( JSON.stringify(item) + "<hr>\n" ); | |
}) | |
res.end(''); | |
} ); | |
}); | |
} | |
var server = connect.createServer( | |
connect.logger(), | |
connect.bodyDecoder(), | |
connect.router(main), | |
connect.errorHandler({ dumpExceptions: true, showStack: true }) | |
).listen(8004); | |
console.log('Connect server listening on port 8004'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment