Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use other node.js libraries in Meteor?

Tags:

meteor

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.

like image 959
SilentDragon Avatar asked Apr 11 '12 20:04

SilentDragon


People also ask

Does meteor use node JS?

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.

How do I use npm on Meteor JS?

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 .

Where are node js libraries stored?

The core modules are defined within the Node.js source and are located in the lib/ folder.

Can you use node packages in browser?

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.


2 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.

like image 130
Adam Blinkinsop Avatar answered Sep 28 '22 09:09

Adam Blinkinsop


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);
like image 44
debergalis Avatar answered Sep 28 '22 08:09

debergalis