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.
Meteor is based on Node. js, thus all Meteor projects are written in JavaScript for both client and server side; that means you don't have to know any other language than JavaScript.
To use an npm package from a file in your application you import the name of the package: import moment from 'moment'; // this is equivalent to the standard node require: const moment = require('moment'); This imports the default export from the package into the symbol moment .
The core modules are defined within the Node.js source and are located in the lib/ folder.
Users can use already existing modules on the client side JavaScript application without having to use a server. But how can this be done? Well, here comes a tool called Browserify. Browserify is a command line NodeJS module that allows users to write and use already existing NodeJS modules that run on the browser.
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
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With