I'm new to node.js (and javascript in general) so I thought I would learn by creating a simple weather app utilizing YQL. Overall, the app is working but the request is extremely slow. It takes about ~6 seconds to return the json. On the other hand, I created the same app using jQuery(getJSON) and I get results almost immediately.
Is this the best way to parse json from an url in node.js?
var request = require('request');
var url = 'http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20location%3D96720&format=json&diagnostics=true&callback='
request(url, function (error, response, body) {
console.log(body);
})
I'd appreciate any feedback and/or suggestions.
Thanks in advance.
I just tried to open that url in my browser and after > 10 seconds, it's still loading. (Update: it timed out.)
My guess is that, Yahoo is setting caching headers that your browser is respecting, so your first request was probably cached and then every jQuery request after that loaded from your browser cache nearly instantly. request() does not have a local cache, so it's downloading a fresh copy each time, which takes much longer.
Try clearing your browser cache and see if it runs as quickly - if the first request is slow then that would confirm my suspicion.
Update: here's some more info about request speeds:
So, I'm starting to think that maybe it's location dependent, and can say at the very least that node isn't slow. If nothing else, you can feel free to proxy requests through my heroku server. Note that it's a free account, so when heroku "idles" it, then the next request is slow or sometimes errors out.