I was playing around with an idea and wanted to get some json from another site. I found with node.js people seem to use http.get to accomplish this however I discovered it wasn't that easy in Meteor. Is there another way to do this or a way to access http so I can call get? I wanted an interval that could collect data from an external source to augment the data the clients would interact with.
3 Answers
Looks like you can get at require this way:
var http = __meteor_bootstrap__.require('http'); Note that this'll probably only work on the server, so make sure it's protected with a check for Meteor.is_server.
4 Comments
Joscha
any idea on how to get transitive dependencies to work? Try aws-lib for example.
Vic Goldfeld
Meteor.is_server is now Meteor.isServer
Christian Schlensker
also add
var require = __meteor_bootstrap__.require; to use the require syntax you're used to.emgee
debergalis's answer should be upvoted now that Meteor.http has been added.
This is much easier now with Meteor.http. First run meteor add http, then you can do something like this:
// common code stats = new Meteor.Collection('stats'); // server code: poll service every 10 seconds, insert JSON result in DB. Meteor.setInterval(function () { var res = Meteor.http.get(SOME_URL); if (res.statusCode === 200) stats.insert(res.data); }, 10000); Comments
You can use Meteor.http if you want to handle http. To add other node.js libraries you can use meteorhacks:npm
meteor add meteorhacks:npm
Create apacakges.json file and add all the required packages name and versions.
{ "redis": "0.8.2", "github": "0.1.8" }