A http server acting as a google reverse geocoder API proxy.
You just need to pass in long/lat as querystrings, for example, "localhost/?hi=there&long=37&lat=37". I put the hi=there in there because node querystring module parses strangely...
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 http = require('http'), | |
q = require('querystring') | |
http.createServer(function (req, res) { | |
//create the geo long/lat path | |
var url=["/maps/geo?q=" | |
,q.parse(req.url)["long"] | |
,"," | |
,q.parse(req.url)["lat"] | |
,"&output=json&sensor=false"].join(""), | |
//create the options to pass into the get request | |
options={host:"maps.google.com" | |
,path:url}; | |
//a little lightweight logging to watch requests | |
console.log("url:",options.host+options.path); | |
console.log("requrl:",req.url); | |
//make the request server side | |
http.get(options,function(response){ | |
//console.log("hi",response); | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
var pageData = ""; | |
response.setEncoding('utf8'); | |
//stream the data into the response | |
response.on('data', function (chunk) { | |
pageData += chunk; | |
}); | |
//write the data at the end | |
response.on('end', function(){ | |
res.write(pageData); | |
res.end(); | |
}); | |
}); | |
}).listen(80); | |
The reason why it's "parsing strangely" is that req.url is not a querystring, but the full url path. Also, you're parsing it more than once, which is a bit unnecessary (though probably harmless).
ReplyDeleteTry this:
```
var url = require('url')
// ... in the server
var u = url.parse(req.url, true)
// now, use u.query.lat, u.query.long
```
why don't you use coffeescript? You're going so far to make an array look decent with commas at the beginning, why put commas in at all?
ReplyDeletei post articles so as to help the greater amount of people online. all coffeescript people know javascript. posting as javascript is more accessible
DeleteAppreciiate you blogging this
ReplyDelete